Peter Schoeller Peter Schoeller - 4 years ago 90
MySQL Question

How to solve entry in MySQL?

I have this php:

echo $_SESSION['user_id']."<br />\n";
$sql = "UPDATE user_passwords SET status ='off' WHERE users_id = 'user_id'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}


output in html notify:

161 // Nr. user from users
Record updated successfully


But NO record in MySQL....
When I modify php like this:

echo $_SESSION['user_id']."<br />\n";
$sql = "UPDATE user_passwords SET status ='off' WHERE users_id ='161'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}


output in html notify:

161 // Nr. user from users
Record updated successfully


and in MySQL is all OK.

Where could be wrong?

Answer Source

Your mistake is here WHERE users_id = 'user_id'";

it should be your session variable in the place of that user_id

<?php

echo $_SESSION['user_id']."<br />\n";

$sql = "UPDATE user_passwords SET status ='off' WHERE users_id = '".$_SESSION['user_id']."'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

?>

NB:Use prepared statements. to protect against sql injections.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download