Sky
Sky

Reputation: 29

How to update mysql db on session destroy

Hello I have question about updating mysql db field with enum 1 and 0 and when the user log in it change it to 1 but when user log out i tried doing this but it won't work... Also I tried make it as button and if btn is pressed do something and then go to logout.php but it didnt work ...

So my code is this ...

<?php include_once('connect.php');
$logger = $_SESSION["login"];
session_start();
session_unset();
session_destroy();


    $mysqli = "UPDATE table SET field='0' WHERE email='$logger'";
    if(mysqli_query($con, $mysqli)){

header("Location: index.php");}else echo "Something went wrong!";
?>

Upvotes: 1

Views: 1381

Answers (3)

Matt
Matt

Reputation: 2869

Your issue isn't to do with the other two answers, @Gopal states that you should destroy after unsetting the session, however you are setting the variable before unsetting the session. @Marmik also doesn't solve the issue.

Your problem lies here:

$logger = $_SESSION["login"];
session_start();

You're trying to access the session variable before you actually start the session. This is going to give you a blank $logger which you may find if you use echo $logger.

So, how do we solve this. Essentially, we just swap two lines around, so that session_start() is before we try accessing the session variables.

session_start();
$logger = $_SESSION["login"];

If this fails, you may want to check that $_SESSION["login"] has actually been set in the first place with a quick echo $_SESSION['login']; after session_start();

Good luck!

Upvotes: 1

Marmik Bhatt
Marmik Bhatt

Reputation: 607

what you can do is you can check the session that he is really logedin or not

On Logout.php

<?php 

 @session_start();

if(!empty($_SESSION['login'])){

include_once('connect.php');

$logger = $_SESSION["login"];



$mysqli = "UPDATE table SET field='0' WHERE email='$logger'";

if(mysqli_query($con, $mysqli)){

    session_unset();
    session_destroy();


    header("Location: index.php");
    }
    else {
         echo "Something went wrong!"
    };

 }
?>

Upvotes: 0

Gopalakrishnan
Gopalakrishnan

Reputation: 957

Give session_unset(); session_destroy(); After update query of the table.

Upvotes: 0

Related Questions