Question

Here is the code

<?
$x=$_SESSION["name"];
mysqli_query($mysqli, "DELETE from Uzivatel WHERE username='$x'");
mysqli_close($mysqli);
header("Location: index.php");
?> 

I saved username into $_SESSION["name"] at the previous page. Here I just load it into $x and I want to delete row from database where username = $x but I can't and I don't know why.

If I change $x in the SQL command into normal string name like Hed (for example) it will delete him. But if I want to delete it through variable, nothing happens.

And yes I checked if the username is in the $_SESSION["name"] through echo, and yes it's there.

Was it helpful?

Solution

You should start the session before you can use a SESSION variable:

<?php
session_start();
$x=$_SESSION["name"];
mysqli_query($mysqli, "DELETE from Uzivatel WHERE username='$x'");
mysqli_close($mysqli);
header("Location: index.php");
?> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top