I'm trying to delete a row in a mySQL table 'users'. It's supposed to delete a users account when a linked button is clicked it takes them to this delete.php page. However, instead of bringing me back to the login page, it keeps me signed in to the account that's supposed to be deleted. I've tried a few other variations however it either doesn't delete the account or returns an error depending on how I wrote it.
<?php
// Initialize the session
session_start();
require_once "config.php";
//delete the mySQL row
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
$sql = "DELETE FROM users WHERE id = 'id'";
mysqli_query($link, $sql);
header("location: login.php");
exit;
}
?>
The same thing happens when I try this:
<?php
// Initialize the session
session_start();
include "config.php";
$id = $_REQUEST["id"];
//delete the mySQL row
$sql="DELETE FROM users WHERE id='$id'";
mysqli_query($sql);
header("location: login.php");
exit;
?>
答案 0 :(得分:5)
请在mysqli_query
中使用链接标识符。
代码应如下:
<?php
// Initialize the session
session_start();
include "config.php";
$id = $_REQUEST["id"];
//delete the mySQL row
$sql="DELETE FROM users WHERE id=".$id;
mysqli_query($link,$sql);
header("location: login.php");
exit;
?>
在重定向之前,您应该使用session_destroy()
或session_unset()
销毁会话。
答案 1 :(得分:1)
在第二个代码中,您忘记将$link
放在mysqli_query($sql)