简体   繁体   中英

Deleting mySQL row with php

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;
?>

Please use link identifier in mysqli_query .

Code should be as below:

<?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;
?>

You should destroy session using session_destroy() or session_unset() before redirection.

在第二个代码中,您忘记将$link放入mysqli_query($sql)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM