无法删除PHP表中的特定行

时间:2019-01-17 02:59:19

标签: php html mysql

每当我尝试删除PHP表中的特定行时,都会不断给我一个错误,而且我不知道为什么。

主PHP

    while($row = mysqli_fetch_array($result))
{
echo "        <tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['usertype'] . "</td>";
echo "<td><a href='delete.php?id=".$row['username']."'>Delete</a></td>";  //if you want to delete based on staff_id
echo "</tr>";
}

delete.php

    <?php
$id = $_GET['id'];
// Include config file
require_once "config.php";


$sql = "DELETE FROM users WHERE username = $id";

if (mysqli_query($link, $sql)) {
    mysqli_close($link);
    header('Location: users');
    exit;
} else {
    echo "Error deleting record";
}
?>

每次尝试删除行时,都会收到“错误删除记录”响应。任何帮助将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:-1)

您似乎移动了错误的变量。在显示的注释中,您可以使用staff_id进行删除,但是,在通过POST传递的变量中,您使用的是username

此外,您的SQL可能正在尝试将usernamestaff_id进行匹配。在这种情况下,请将传出ID更改为

echo "<td><a href='delete.php?id=".$row['staff_id']."'>Delete</a></td>";

将sql更改为:

$sql = "DELETE FROM users WHERE staff_id= ".$id;

HTH

答案 1 :(得分:-1)

$sql = "DELETE FROM `users` WHERE `username` = '$id'"

否则,$id将被视为表users的一列。