Can't get value from post method to SQL query - php

spent long hours trying to add delete functionality to my to-do list. I can add todo to sql, also it apears on page, but cant find a way to delete my todo. Maybe someone can help me? here's the code:
<!DOCTYPE html>
<html>
<head>
<title>To Do</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="task" placeholder="New task"/>
<input type="submit" name="add" value="Add" autocomplete="off" required/>
</form>
<?php
if (isset($_POST['add'])) {
$sql = "INSERT INTO list (todo) VALUES ('$_POST[task]')";
$conn->query($sql);
}
if(isset($_POST['delete'])){
$sql ="DELETE FROM list WHERE todo={$_POST['todo']}";
$conn->query($sql);
}
$query = "SELECT * FROM list";
$result = mysqli_query($conn, $query);
?>
<?php
while ($row = mysqli_fetch_assoc($result)) { ?>
<?php echo $row['todo'] ?>
<form action='index.php?todo="<?php echo $row['todo']; ?>"'method="post">
<input type="hidden" name="todo" value="<?php echo $row['todo']; ?>">
<input type="submit" name="delete" value="Delete">
</form> <br>
<?php }
$conn->close();
?>
</body>
</html>

Related

Insert data on mysql table

I'm not able to insert the data with this.
Here is my code:
<?php
include('includes/config.php');
if(isset($_POST['update'])) {
$sampleid=$_POST['sampleid'];
$aba11=$_POST['aba11'];
$aba12=$_POST['aba12'];
$sql="INSERT INTO 'aba1'(sampleid,aba11,aba12) VALUES(:sampleid,:aba11,:aba12)";
$query = $dbh->prepare($sql);
$query->bindParam(':sampleid',$sampleid,PDO::PARAM_STR);
$query->bindParam(':aba11',$aba11,PDO::PARAM_STR);
$query->bindParam(':aba12',$aba12,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
}
?>
<html>
<form>
<body>
<label> Sampleid </label> <input type="text" name="sampleid"><br>
<label> Start time </label> <input type="text" name="aba11"><br>
<label> Stoptime </label> <input type="text" name="aba12"><br>
<button type="submit" name="update" >Update</button>
</Form>
</body>
</html>
My database connection is correct. There is no error in config.php file.
Your form's method was missing and the value in your update button was missing
also, you must use ` instead of ' for database table names
try the code below
<?php
include('includes/config.php');
if(isset($_POST['update'])) {
$sampleid=$_POST['sampleid'];
$aba11=$_POST['aba11'];
$aba12=$_POST['aba12'];
$sql="INSERT INTO `aba1` (sampleid,aba11,aba12) VALUES(:sampleid,:aba11,:aba12)";
$query = $dbh->prepare($sql);
$query->bindParam(':sampleid',$sampleid,PDO::PARAM_STR);
$query->bindParam(':aba11',$aba11,PDO::PARAM_STR);
$query->bindParam(':aba12',$aba12,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
}
?>
<html>
<body>
<form method="POST" action="">
<label> Sampleid </label> <input type="text" name="sampleid"><br>
<label> Start time </label> <input type="text" name="aba11"><br>
<label> Stoptime </label> <input type="text" name="aba12"><br>
<button type="submit" name="update" value="1">Update</button>
</Form>
</body>
</html>
Code seems pretty much ok for me.
Are you certain that $_POST['update'] is set to something?
The lack of error could indicate that it isn't even going through your PHP block.
use form method post
<form method="post" id="insert-data">
</form>
value in insert query put $sampleid,$aba11,$aba12

i want to update my database

1-now already i added some texts on my database then after i want to update my database
anybody can help me?
i hope someone can help me
<?php
mysqli_query($db,"INSERT INTO tasks (task,status)
VALUES ('$task','$status')");
header('location:SIMPLE.PHP');
}
}
if (isset($_GET['check_selected'])) {
foreach ($_GET['check_selected'] as $key => $value) {
$row = mysqli_query($db,"DELETE FROM tasks WHERE id =
$value;") or die(mysqli_error($db));
}
}
$tasks = mysqli_query($db,"SELECT * FROM tasks ORDER BY id DESC");
?>
<!DOCTYPE html>
<html>
<body>
<form method="GET" action="SIMPLE.PHP">
<input type="text" name="search" class="status_input" placeholder="TEXT" />
<input id='textbox' type='checkbox' value='1' name='status'>
<button type="submit" class="add_btn" name="submit">Add</button>
<p><?php echo $errors; ?></p>
</form>
<form action="SIMPLE.PHP" method="GET">
<tbody>
<tr>
<p><button type="submit" class="btn btn-success" name="save">DELETE</button></p>
<?php
foreach ($rows as $row_id => $row)
{
?>
<input name="check_selected[]" type="checkbox" value=<?php echo $row['id'];?>"/>
<input name = "checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['status'];?>"
<?PHP if($row['status']){echo 'checked';
}
?>
>
<td class= "task"><?php echo $row['task']?></td>
<?PHP
}
?>
</tr>
</tbody>
</form>
</body>
</html>

Why are my search results not visible under the search form?

hoping somebody is able to help!
I have created a search form to enable a user to search for a specific assessment day using the name, date and the company it is for.
The results of this search should appear below the form used to generate the search. However, at the moment, when the search button is selected the form just refreshes and does not show any results?
Can anyone advise? Code below:
<?php
if(isset($_GET['submit'])){
require_once 'connect.php';
if(isset($_GET['nameofassessmentday'])) {
if(isset($_GET['dateofassessmentday'])) {
if(isset($_GET['companyname'])) {
$nameofassessmentday = $db-> real_escape_string($_GET['nameofassessmentday']);
$dateofassessmentday = $db-> real_escape_string($_GET['dateofassessmentday']);
$companyname = $db-> real_escape_string($_GET['companyname']);
$query = $db->query ("
SELECT Name, Company
FROM assessment_day_details
WHERE Name LIKE '{$nameofassessmentday}'
AND Company LIKE '{$companyname}'
AND Date_of_Day = '{$dateofassessmentday}'
");
}
}
}
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results.
</div>
<?php
if($query-> num_rows) {
while($r = $query->fetch_object()) {
?>
<div class="result">
<?php echo $r->Name; ?>
<?php echo $r->Company; ?>
</div>
<?php
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Assess Existing Assessment Day-Search</title>
<link rel="stylesheet" href="style.css">
</head>
<?php
include 'function.php';
}
?>
<body>
<div id="form">
<form method="get">
<p>
<label>Name of Assessment Day:</label>
<input type="text" id="nameofassessmentday" name="nameofassessmentday" required/>
</p>
<p>
<label>Date of Assessment Day:</label>
<input type="date" id="dateofassessmentday" name="dateofassessmentday" required />
</p>
<p>
<label>Company Name :</label>
<input type="text" id="companyname" name="companyname" required/>
</p>
<p>
<input type="submit" id="btn" value="Search" />
</p>
</form>
</div>
</body>
</html>
Also, just one additional question for future reference. Is there anyway I would be able to hyperlink a search result to go to a particular page?
Thanks in advance!
And as some further information, prior to trying to get the search results to show in the same webpage, I had the below two pages and it worked perfectly.
<!DOCTYPE html>
<html>
<head>
<title>Assess Existing Assessment Day-Search</title>
<link rel="stylesheet" href="style.css">
</head>
<?php
include 'function.php';
?>
<body>
<div id="form">
<form action="search.php" method="get">
<p>
<label>Name of Assessment Day:</label>
<input type="text" id="nameofassessmentday" name="nameofassessmentday" required/>
</p>
<p>
<label>Date of Assessment Day:</label>
<input type="date" id="dateofassessmentday" name="dateofassessmentday" required />
</p>
<p>
<label>Company Name :</label>
<input type="text" id="companyname" name="companyname" required/>
</p>
<p>
<input type="submit" id="btn" value="Search" />
</p>
</form>
</div>
</body>
</html>
<?php
require_once 'connect.php';
include 'function.php';
if(isset($_GET['nameofassessmentday'])) {
if(isset($_GET['dateofassessmentday'])) {
if(isset($_GET['companyname'])) {
$nameofassessmentday = $db-> real_escape_string($_GET['nameofassessmentday']);
$dateofassessmentday = $db-> real_escape_string($_GET['dateofassessmentday']);
$companyname = $db-> real_escape_string($_GET['companyname']);
$query = $db->query ("
SELECT Name, Company
FROM assessment_day_details
WHERE Name LIKE '{$nameofassessmentday}'
AND Company LIKE '{$companyname}'
AND Date_of_Day = '{$dateofassessmentday}'
");
}
}
}
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results.
</div>
<?php
if($query-> num_rows) {
while($r = $query->fetch_object()) {
?>
<div class="result">
<?php echo $r->Name; ?>
<?php echo $r->Company; ?>
</div>
<?php
}
}
?>
you have to add '%' when you use LIKE in query
check here https://www.w3schools.com/sql/sql_like.asp

Crud functionality, Trying to update user details with php and mysql

I'm new to php and I have a login system and an account which allows users to edit their details however when they fill in the form the database does not update and my error message does not show? Can anyone help?
accountamend.php
<?php
include 'connection.php'; // this includes my connection to the database
$ID=$_GET['ID'];
$query="SELECT ID, email, password, FROM users WHERE ID=$ID";
$result=mysqli_query($connection,$query) or die (mysqli_error($connection));
if (mysqli_num_rows($result)>0){
$row=mysqli_fetch_assoc($result);
} else {
$row=NULL;
}
?>
<html>
<head>
<title> Update Details </title>
<link rel="stylesheet" href="homecss.css"/>
<link href='http://fonts.googleapis.com/css?family=Nunito:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
<form method ="post" action="amendaccount.php">
<fieldsetclass="fieldset-width">
<legend>
Enter New Details
</legend>
<input type="hidden" name"ID" value="<?php echo $ID; ?>" />
<label for="email">Email: </label>
<input type = "text" name="email" value="<?php echo $row['email']; ?>" />
<br/>
<label for ="password"> Password: </label>
<input type = "password" name="password" value="<?php echo $row['password']; ?>" />
</fieldset>
<input type="submit" value="submit" name="submit" />
<input type="reset" value="clear" />
</form>
</body>
</html>
editaccount.php
<?php
include 'connection.php';
$ID=$_POST['ID'];
$email=$_POST['email'];
$password=$_POST['password'];
$query="UPDATE users SET email='$email', password='$password' WHERE ID='$ID'";
mysqli_query($connection,$query);
echo "Update Success";
?>
Looks like you have an error in your code here:
<input type="hidden" name"ID" value="<?php echo $ID; ?>" />
Should be:
<input type="hidden" name="ID" value="<?php echo $ID; ?>" />
in your editaccount.php you could add something like
printf("Errormessage: %s\n", mysqli_error($connection));
after your mysqli_query statement so you could see the latest error (if any).
Did you output your $query and try to execute it by hand? Maybe this shows you an error as well?

Adding posts to from form database categories

I have a form from where i add posts to my database table and i made categories in this table and i puted in the "ADD POSTS" form an option to choose the category when i add the post. It works in my other website witch is just to try some new things but to my main project it doesnt and i use the same code with same tables names. Can someone check the code and tell me where is my mistake?
Thats my whole ADDposts form:
<?php
include 'includes/connect.php';
if(isset($_POST['add']))
{
$time = time();
$title = htmlspecialchars($_POST['title']);
$content = strip_tags($_POST['content']);
$post_image= $_FILES['image'] ['name'];
$image_tmp= $_FILES['image'] ['tmp_name'];
$q = "INSERT INTO posts(post_title,post_content,post_author,added,post_image) VALUES('$title','$content','Papazov','$time','$post_image')";
mysql_query($q) or die (mysql_error());
}
?>
<!DOCTYPE html>
<?php include "hhh.html"; ?> <br /><br />
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MatchZone</title>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
</head>
<body>
<p>
<form method="post" action="Addpost.php" enctype="multipart/form-data">
Заглавие: <input type="text" name="title" /><br/><br/>
<tr>
<td align="right">Preview:</td>
<td><input type="text" name="preview" size="30"></td>
</tr>
<br />
<br />
<tr>
<td align="right">Сложи снимка: </td>
<td><input type="file" name="image"></td>
</tr>
<br/><br/>
Категория: <select name="category">
<?php
$q = mysql_query("SELECT*FROM categories") or die (mysql_error());
while($c = mysql_fetch_assoc($q))
{
print '<option value="'.$c['cat_id'].'">'.$c['name'].'</option>\n';
}
?>
</select><br />
<br />
<textarea class="ckeditor" name= "content" cols="30" rows="10"></textarea><br />
<input type="submit" name="add" value="Добави" />
</form>
</p>
</body>
</html>
Did you close the select tag? If you can show the part where you insert to the database would be of help
This looks to me like you're trying to run everything off the same page.
If this is the case (which most likely is), then change action="Addpost.php" to action=""
Since you have if(isset($_POST['add'])) near the top of your code,
and further below <input type="submit" name="add" value="Добави" /> clearly being in the same page.

Categories