我的页面集signin_time
将其设置为NOW()
,当用户成功登录页面时将logged_in
设置为1。我想要做的是在用户退出时将signout_time
设置为NOW()
。为此我正在使用以下查询
$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);
我尝试执行此查询。它没有返回任何错误消息,但我没有注意到db表中的任何更改,也没有php错误日志显示任何内容。
也许我有语法错误(我正在设置logged_in=0 where logged_in=1
)。我不知道该怎么做。也许我应该搜索signout_time=0
的行?有什么建议?
这是整个功能:
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);
}
答案 0 :(得分:1)
您必须执行查询。
$userId = 1; // some number
$stmt->execute(array($userId));
或者您可以使用方法链接将它们组合成一个:
$db->prepare(
"UPDATE `ulog` SET `logged_in`=0, `signout_time`=NOW(),
`ckey`= '', `ctime`= '' WHERE user_id=? AND logged_in=1"
)->execute(array($userId));
另外,请确保正在设置$userid
。在它上面做var_dump()
,看看你得到了什么。
答案 1 :(得分:0)
你有没有尝试过不同的准备/执行方法,看起来我和你的方法略有不同......不是说它肯定会有用但我觉得值得一试
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` = :userid
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` = :userid
AND `logged_in` = 1") or die($db->error);
}
$dataArray = array(':userid' => $userid);
$stmt->execute($dataArray);
$stmt->close();
$_SESSION = array();
session_destroy();
foreach ($_COOKIE as $c_id => $c_value) {
setcookie($c_id, '', 1, "/");
}
header("Location: " . $wsurl);
}