注销时更新sql表

时间:2016-02-25 17:44:58

标签: php session mysqli logout

我想在成功注销时更新表字段。但我的代码似乎没有用。

以下是我的代码。它会破坏会话但不会更新表字段。

<?php
session_start();
if (session_destroy()) {
    include "lib/session.php";
    include "lib/mysql.php"; 
    $session_id = $_SESSION['grammarly_sessid'];
    $q=mysqli_query("update online_status set status='OFF' where id = '$session_id'");
} else {
    exit;
}

unset($_SESSION['grammarly_sessid']);
session_start();
session_destroy();
header('location:../login.php')
?>

请帮忙。

2 个答案:

答案 0 :(得分:0)

您需要在销毁会话之前获取会话ID。

如果你没有将它作为OOP对象使用,你还需要将MySQLi标识符传递给mysqli_query()函数的第一个参数。

<?php
session_start();
$session_id = $_SESSION['grammarly_sessid'];

$mysqli = mysqli_connect('localhost', 'mysql_user', 'mysql_pass');
mysqli_select_db($mysqli, 'database_name');

if(session_destroy())
{
include "lib/session.php";
include "lib/mysql.php"; 
$q=mysqli_query($mysqli, "update online_status set status='OFF' where id = '$session_id'");
}
 else{
exit;
}

header('location:../login.php')
?>

答案 1 :(得分:0)

session_start();
include "lib/session.php";
include "lib/mysql.php"; 
$session_id = $_SESSION['grammarly_sessid'];
$q          = mysqli_query("update online_status set status='OFF' where id = '$session_id'");    
unset($_SESSION['grammarly_sessid']);
session_destroy();
header('location:../login.php');

在这种情况下,您不必添加条件..