gpt4 book ai didi

php - 如何删除选定的用户?磷酸二氢钾

转载 作者:行者123 更新时间:2023-11-29 20:27:58 26 4
gpt4 key购买 nike

我正在使用 PDO,并且我想从我的成员列表中删除指定的用户。我制作了“删除”按钮以显示在每个用户上,并且我希望当我单击任何随机用户时删除他。这是我尝试做到这一点的第二天。

我做了这样的事情:

$id = $user->id;
$sql = "DELETE FROM `users` WHERE `id` = :id";
$query = $handler->prepare($sql);
$query->execute(array(":id" => $id));

但这不是一个解决方案,这将删除所有加入该页面的人。我想在单击“删除”按钮时删除该按钮所在的用户。希望您能理解我,我会向您展示我想要的图片。

enter image description here

最佳答案

您应该使用 $_POST 全局数组获取要删除的用户 ID($_REQUEST 可以,但可能会让您面临更多 XSS 攻击的风险 - 这是因为它也接受 GET 变量,这些变量可以来自表单外部)。

我会找出你哪里出了问题。

根据您放置的 HTML 检索用户 ID。

// This user id can be obtained in two ways as I suggested.
// 1.) $_REQUEST['user_id'] if you pass the data in the href link
// 2.) $_POST['user_id'] if you pass the data as hidden field in form element.
// 3.) Ensure that you get your USER ID correct in that place by echoing the query that you have made in the SQL.
profile.php
<a href="process.php?delete_id=<?php echo $data->id; ?>">DELETE</a>

流程.php

<?php
// Ensure DB connectivity in this page. And this code will work fine.
if(isset($_REQUEST['delete_id'])){
$id = $_REQUEST['delete_id'];
$sql = "DELETE FROM `users` WHERE `id` = :id";
$query = $handler->prepare($sql);
$query->execute(array(":id" => $id));
}
?>

关于php - 如何删除选定的用户?磷酸二氢钾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39188786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com