I want to update a table field on successful logout. But my code doesnt seem to work.
Below is my code. It destroys the session but it doesnt update the table field.
<?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')
?>
Please help.
You need to fetch the session ID before you destroy the session.
You also need to pass the MySQLi identifier to the first parameter of the mysqli_query()
function if you are not using it as an OOP object.
<?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')
?>
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');
here in this case, you don't have to add a condition..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.