Harry Pen Harry Pen - 1 year ago 73
HTML Question

how to add a MySQL Command to every page

I have a code I would like to run on every page, however it doesn't seem to work at all.

include 'db.php';
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "User";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);

$sql = "UPDATE users SET login_time=NOW() WHERE id='$id'";

if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;


I have also tried the line

$sql = "UPDATE users SET login-time='NOW()' WHERE id='$id'";

It comes back saying
Record updated successfully
But doesn't acctually update.

Thank you

Answer Source

This will never work:

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

query() calls either return a statement handle/object (success), or a boolean FALSE (failure). They will never return a boolean TRUE, meaning that this comparison can never ever succeed. You should never have gotten a "success" message, only the "failed" one.

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