在PHP中从数据库中删除帐户

在PHP中从数据库中删除帐户,php,mysql,sql,database,Php,Mysql,Sql,Database,当管理员单击下图中的“删除”按钮时,我正试图删除一个帐户: Delete.php <?php $con=mysqli_connect("localhost","root","123","data1"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="Delete from users where

当管理员单击下图中的“删除”按钮时,我正试图删除一个帐户:

Delete.php

<?php
$con=mysqli_connect("localhost","root","123","data1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="Delete from users where id=id";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Record Deleted";
header("Refresh:3; url=admin.php");

mysqli_close($con);
?>
这是我在图片中使用的代码

Admin.php

<?php
$con=mysqli_connect("localhost","root","123","data1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM users");

echo "<table align='center' id='rounded-corner'>";
echo "<thead>
        <tr>
        <th scope='col' class='rounded-company'>ID</th>
        <th scope='col' class='rounded-company'>Username</th>
        <th scope='col' class='rounded-q1'>Password</th>
        <th scope='col' class='rounded-q2'>Filter Status</th>
        <th scope='col' class='rounded-q3'>Led Status</th>
        <th scope='col' class='rounded-q4'>Heater Status</th>
        <th scope='col' class='rounded-q4'>Edit</th>
        <th scope='col' class='rounded-q4'>Delete</th>
        </tr>
    </thead>";
    echo "<tfoot>";
echo "</tfoot>
<tbody>";

while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['filter_st'] . "</td>";
echo "<td>" . $row['led_st'] . "</td>";
echo "<td>" . $row['heat_st'] . "</td>";
echo "<td>" . '<a class="button_green" href="edit.php">Edit</a>' . "</td>";
echo "<td>" . '<a class="button_red" href="delete.php">Delete</a>' . "</td>";
    echo "</tr>";
}
echo "</tbody>
</table>";

mysqli_close($con);
?>
$sql=从id=id的用户中删除


未定义id。

按照您当前的方式,它将删除您的所有用户。因为对于每个用户,id将等于id

它需要改成

$sql="Delete from users where id=$id";

这样,它只会删除id=$id的用户,因为他们没有足够的信息来给出完整的答案,但有一点需要注意:

$sql="Delete from users where id=id";
事实上,一个id总是等于它本身,因此您将有效地删除每个用户

您需要区分ID

$id = 10; //(get the id somehow)
$sql="Delete from users where id=". $id;
或者更简单地说

$id = 10; //(get the id somehow)
$sql="Delete from users where id=$id";

首先要将用户id作为参数添加到编辑/删除操作中,但确保在这些文件中强制执行授权;对于删除,这将给出

echo "<td>" . '<a class="button_red" href="delete.php?id=" . $row['id'] . ">Delete</a>' . "</td>";
请注意,您需要验证并取消指定从$\u GET数组中的查询参数恢复的数据,当然还要确保只有授权用户才能有效地执行这些操作,因此您需要检查请求删除的用户是否经过身份验证并具有适当的授权,你不能假设只有管理员知道这个url

现在,正如其他人所指定的,您需要正确地计算删除查询

"DELETE FROM users where id = " . $id;
或者准备一份声明

$stmt = mysqli_prepare($con, "DELETE FROM users where id = ?;");
mysqli_stmt_bind_param($stmt, 'd', $id); // you bind one numeric parameter ('d') with $id as value
mysql_stmt_execute($stmt);

编辑:该死,我刚刚通过“相关”问题看到了这个问题,我问了三个月了……

你有问题吗?是的,当单击“删除”按钮时,我应该如何获得我要删除的特定用户的id?@user2997404-你的屏幕截图中有生成表单的代码吗?这就是我们需要看到的代码@user2997404@Fred-它不起作用。
$stmt = mysqli_prepare($con, "DELETE FROM users where id = ?;");
mysqli_stmt_bind_param($stmt, 'd', $id); // you bind one numeric parameter ('d') with $id as value
mysql_stmt_execute($stmt);