-2

How do i actually update the values of table using PHP ? This code is not showing any error and its not updating either.

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'DB';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_error()) 
{
die("couldn't connect" . $conn->connect_error());
}
echo ("connected successfully");
$id = $_POST['Id'];
$name = $_POST['Name'];
$dept = $_POST['Department'];
$update = "update info set Name='$name', Department='$dept' where Id='$id'";
if($conn->query(update) === TRUE) {
echo ("Data updated successfully");
} 
else
{
echo ("Data cant be updated" . $conn->error());
}
$conn->close();
?>
Naga Naveen
  • 99
  • 1
  • 4
  • 16
  • 1
    Show more code please. – Jens Apr 13 '15 at 05:41
  • Have you checked the column names if they are correct? Is there a row matching with the bind `$id`? Did you execute your query? Show more code. – Logan Wayne Apr 13 '15 at 05:42
  • how you are executing your query in php ? – Ayyanar G Apr 13 '15 at 05:43
  • connect_error()); } echo ("connected successfully"); $id = $_POST['Id']; $name = $_POST['Name']; $dept = $_POST['Department']; $update = "update info set Name='$name', Department='$dept' where Id='$id'"; if($conn->query(update) === TRUE) { echo ("Data updated successfully"); } else { echo ("Data cant be updated" . $conn->error()); } $conn->close(); ?> – Naga Naveen Apr 13 '15 at 05:45
  • Update this with your original post. Don't comment it. – Logan Wayne Apr 13 '15 at 05:46
  • Yes. The column names are perfect. I would've tried at least 10 times. I don't know what is the problem. i'm pretty new to this. – Naga Naveen Apr 13 '15 at 05:46
  • Post this to your question and not here in the comment area. – Amit Verma Apr 13 '15 at 05:47
  • First of all, are you sure the DB user you use to connect from php has the update privileges? - EDIT: seen your updated question. I think your root has all the privileges, even if it's not very safe to use it. – Marco Bernardini Apr 13 '15 at 05:49
  • Sorry. And yes the privileges are fine. But can you people tell me how to check those? I may have a look now. I'm using XAMPP – Naga Naveen Apr 13 '15 at 05:51

5 Answers5

4

Hope this one help you!

$update = "update info set Name='".$name."', Department='".$dept."' where Id='".$id."'";
3

Check this part of your code:

if($conn->query(update) === TRUE) {

where it should be:

if($conn->query($update) === TRUE) {
  • Make sure that you are using the correct credentials (host, username, password, database name) according to your MySQL database.
  • Also your table name and column name should be correct which are being used in your query.
  • Make sure that there is a match with your condition part of your query (... WHERE Id='$id'). Check it by running a query in your PhpMyAdmin page, or Search the ID, which is also the one you try to input in your form.
  • Make sure that the name of the passed variables ($_POST[]) are correct.
  • Be case sensitive.

Try changing your connection into:

$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

Other way to execute your query is to simply:

mysqli_query($conn,$update);

Recommendation:

You should escape the values of your variables before using them into your query by using mysqli_real_escape_string() function:

$name = mysqli_real_escape_string($conn,$_POST["Name"]);

Or better, so you won't need to worry about binding variables into your query and as well prevent SQL injections, you should move to mysqli_* prepared statement:

if($stmt = $conn->prepare("UPDATE info SET Name=?, Department=? WHERE Id=?")){

  $stmt->bind_param("ssi",$_POST['Name'],$_POST['Department'],$_POST['Id']);
  $stmt->execute();
  $stmt->close();

}
Community
  • 1
  • 1
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
1
$update = "update info set Name='".$name."', Department='".$dept."' where Id='".$id."'";
 mysql_query($update);
Amitesh Yadav
  • 202
  • 1
  • 10
1

$update = "update info set Name='".$name."',set Department='".$dept."' where Id='".$id."'";

if this is not help please provide form code.

Sourabh
  • 500
  • 2
  • 14
0

Try this

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'DB';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(!$conn) 
{
die("ERROR CONNECTING TO DATABASE!");
}
echo "Connected Successfully";
$id = $_POST['Id'];
$name = $_POST['Name'];
$dept = $_POST['Department'];
$update = "update info set Name='$name', Department='$dept' where Id='$id'";
$qry = mysqli_query($conn,$update);
if(!$qry) {
echo "Error Updating Details".mysqli_error($conn);
} 
else
{
echo "Data updated successfully";
}
mysqli_close($conn);
?>

(Optional) Use secure things. Change to this for more secure.

$id = mysqli_real_escape_string($conn,$_POST['Id']);
$name = mysqli_real_escape_string($conn,$_POST['Name']);
$dept = mysqli_real_escape_string($conn,$_POST['Department']);