简体   繁体   中英

PHP MySQL UPDATE - no errors, but not updating

I am having trouble with what seems like it should be a simple MySQL update. However when I check in phpMyAdmin, the row is not updating. I have a table with columns:

| id | email | item1 | item2 | item3 |

  • item1, item2, and item3 are all set to null.
  • id is generated when the user enters their email as the user completes a task, their result is entered into the corresponding item column.
  • Before they complete the task the code checks to see if they have done so by checking for a null value.
  • If the value is null they can continue to the task, then it goes to this code to store the result.

     require("login.php"); $connection = mysqli_connect($servername, $username, $password, $dbname); $id = "1"; $name = "item1"; $file = mysqli_real_escape_string($connection, $fileName); $sql = "UPDATE once SET ".$name." = '".$file."' WHERE id = ".$id; //I also tried: //$sql = "UPDATE once SET ".$name." = '".$file."' WHERE id = '".$id."'"; echo $sql; $result = mysqli_query($connection, $sql); mysqli_close($connection); 

I did an echo statement to show that this is what the $sql line is sending:

UPDATE once SET item1 = 'New File!' WHERE id = 1

(alternate with id = '1' also sent as expected)

I've checked other people's questions here and have tried altering mine to fit the accepted answers... but each time, phpMyAdmin continues to show no updates. I am showing no errors in my error_log file any more and have no idea how to continue.

UPDATE: Problem solved I used a person's nickname instead of their proper name in my table column... I feel pretty silly right now.

Lesson learned: more error reporting! This line saved me:

   echo $result ? 'good' : 'failed with ' . mysqli_error($connection);

Use affected_rows to see if something was updated:

$connection->affected_rows;  

http://php.net/manual/en/mysqli.affected-rows.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM