Reputation: 9880
I have the following php code that updates the sql table with the edited data that is posted from the form:
$user_input_id = $_POST['id'];
$user_input_abc = $_POST['abc'];
$user_input_def = $_POST['def'];
$user_input_xyz = $_POST['xyz'];
$sqlTable = 'abc_table';
$stmt = $mysqli->prepare("UPDATE $sqlTable SET abc=?, def=?, xyz=? WHERE id=?");
$rc = $stmt->bind_param("sssi", $user_input_abc, $user_input_def, $user_input_xyz, $user_input_id);
$stmt->execute();
echo 'Your information was updated to the database.';
$stmt->close();
$mysqli->close();
When I edit the form and submit the change, the php page with the above script shows that "Your information was updated to the database." but when I check the database, the edits are not updated and its still showing the old data.
I also tried echoing the variables like echo $user_input_id, $user_input_abc, $user_input_def, $user_input_xyz;
to make sure the variables do have the edited texts that is posted from the from in the previous page and they seem right. Its just that the update query that is supposedly showing successful is not updating the edited texts in my sql table.
Is there anything wrong with my code?
Upvotes: 0
Views: 3298
Reputation: 9880
It was the $_POST['id']
that was causing the problem for me. The previous form page that passes the ID to this update.php file had <input type="hidden" name="id" value="<? echo $id ?>" />
instead of <input type="hidden" name="id" value="<?php echo $id ?>" />
. I missed the php
after <?
. Because of that, the ID that was posted was empty. I did have error checking for prepare() failed
, bind_param() failed
and execute() failed
(although I only showed the brief code above without the error checking lines), it still didint show any errors and the query was showing as successful. This is caused due to empty id field, so the query was successful and didint show any errors but since the id was empty no rows in table was updated. Once I made sure that the id is posted correctly, everything else worked. (Silly mistake caused me a headache)
Thank you all for your input that helped me in noticing my mistake.
Upvotes: 1