0

Like the title says how do i delete email when user logs out. I have this code but it doesn't work.

<?php

    session_start();

    $email = $_SESSION['email'];

     $sql = "DELETE FROM onlineusers WHERE email = $email";
     mysqli_query($link, $sql);

    session_destroy(); 

    header("Location: index.php");
?>
ThisDude
  • 13
  • 4
  • 1
    That should be giving you query error. You have to wrap the email in quotes `... email = '$email'"`;`. **Huge BUT** here: look for **prepared statements** instead. – FirstOne May 21 '17 at 12:34
  • For reference: [**How can I prevent SQL injection in PHP?**](http://stackoverflow.com/q/60174/4577762) – FirstOne May 21 '17 at 12:37

2 Answers2

0

This should be your query

$sql = "DELETE FROM onlineusers WHERE email = '$email'";

Note the '$email' instead of $email.

Pritpal Singh
  • 62
  • 1
  • 12
0
  1. I assume that database connection is successful when you are on this script.
  2. The only error I see is that $email needs to in single quotes as it might be of varchar type field in database.

    $sql = "DELETE FROM onlineusers WHERE email = '$email'";

  • I put $email in single quotes but it still doesn't work. Maybe if i explain what am i doing here will help. So when user logs in his email gets stored into onlineusers table in database and when he logs out his email gets deleted. – ThisDude May 21 '17 at 13:01
  • Did you check my first comment regarding connection to DB? You are using $link in mysqli_query, which I assume that your script is connecting with database. – Danish Hakim Khan May 21 '17 at 13:09
  • Yes connection is fine. – ThisDude May 21 '17 at 13:19