Ethan Guillotte Ethan Guillotte - 1 year ago 59
PHP Question

Can't record current time-stamp for logging out

So I'm building a simple account system using PHP and mySQL. For some reason, I just can't get the current timestamp to record to the database when the user logs out.

Here's my logout script:

<?php require 'connections.php'; ?>

//Setting Last Online Time
$QueryLO = "UPDATE account SET A_LastOnline = date() WHERE AID='".$row['AID']."'";

header('Location: index.php');

The format of A_LastOnline is DateTime.

Any help is much appreciated!

Edit: Here's the query working in the php portion of my login page.

<?php require 'connections.php'; ?>


$UN = $_POST['Username'];
$PW = $_POST['Password'];

$result = $con->query("select * from account where A_Username='$UN' AND A_Password='$PW'");

$row = $result->fetch_array(MYSQLI_BOTH);


$_SESSION["AID"] = $row['AID'];
$_SESSION["A_UserLevel"] = $row['A_UserLevel'];
$_SESSION["A_Username"] = $row['A_Username'];
$_SESSION["A_Screenname"] = $row['A_Screenname'];
$_SESSION["A_FirstLogin"] = $row['A_FirstLogin'];
$_SESSION["A_RegisteredIP"] = $row['A_RegisteredIP'];
$_SESSION["A_LatestIP"] = $row['A_LatestIP'];
$_SESSION["A_LastOnline"] = $row['A_LastOnline'];

echo $QueryLO = "UPDATE account SET A_LastOnline = NOW() WHERE AID='".$row['AID']."'";



Edit: So my final solution was that:

$QueryLO = "UPDATE account SET A_LastOnline = date() WHERE AID='".$row['AID']."'";

Had to be changed to:

echo $QueryLO = "UPDATE account SET A_LastOnline = NOW() WHERE AID='".$_SESSION["AID"]."'";

To account for the session variable, since my $row wasn't set on this particular page.

Answer Source

1st: Replace date() with NOW() in your query (or CURDATE() if you don't care about the specific time of day)

2nd: confirm that the query was run successfully by changing your query line to

if(!$con->query($QueryLO)){/*handle error here*/}
else{/*success: confinue to logout*/}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download