This question already has an answer here:
My page sets signin_time
sets it to NOW()
, sets logged_in
to 1 when user successfully signs into page. What I want to do is to set signout_time
to NOW()
when user signs out too. For this purpose I'm using following query
$stmt = $db->prepare("UPDATE `ulog` SET `logged_in`=0, `signout_time`=NOW(),`ckey`= '', `ctime`= '' WHERE user_id=? AND logged_in=1") or die($db->error);
I tried to execute this query. It didn't return any error message, but I haven't noticed any change in db table too, also php error log doesn't show anything.
Maybe I have syntax error (I'm setting logged_in=0 where logged_in=1
). I have no other idea how to do that. Maybe I should search for rows where signout_time=0
? Any suggestions?
Update
Here is whole function:
function logout() {
global $db, $wsurl;
if (isset($_SESSION['user_id'])) {
$userid = $_SESSION['user_id'];
$stmt = $db->prepare("UPDATE `ulog` SET `logged_in`=0, `signout_time`=NOW() WHERE user_id=? AND logged_in=1") or die($db->error);
} else {
$userid = $_COOKIE['user_id'];
$stmt = $db->prepare("UPDATE `ulog` SET `logged_in`=0, `signout_time`=NOW(),`ckey`= '', `ctime`= '' WHERE user_id=? AND logged_in=1") or die($db->error);
}
$stmt->bind_param("i", $userid) or die($stmt->error);
$stmt->execute() or die($stmt->error);
$stmt->close();
$_SESSION = array(); //destroy all of the session variables}
session_destroy();
foreach ($_COOKIE as $c_id => $c_value) {
setcookie($c_id, '', 1, "/");
}
header("Location: " . $wsurl);
}
Have you tried different methods of preparing/executing, it seems I do mine slightly different to yours... not say it will definitely work but it's worth a try I guess
You have to execute the query.
Or you could combine them into one using method chaining:
Also, make sure your
$userid
is being set. Do avar_dump()
on it and see what you get.