Harry Pen Harry Pen - 7 months ago 10
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.

<?php
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;
}

$conn->close();
?>


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

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.

Comments