Ethan Guillotte Ethan Guillotte - 4 months ago 14
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'; ?>
<?php
session_start();

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

unset($_SESSION["AID"]);
unset($_SESSION["A_UserLevel"]);
unset($_SESSION["A_Username"]);
unset($_SESSION["A_Screenname"]);
unset($_SESSION["A_FirstLogin"]);
session_destroy();
header('Location: index.php');
exit();
?>


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

if($_SERVER["REQUEST_METHOD"] == "POST"){

$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_start();

$_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']."'";
$con->query($QueryLO);

}

?>


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

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*/}
Comments