作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 PDO,并且我想从我的成员列表中删除指定的用户。我制作了“删除”按钮以显示在每个用户上,并且我希望当我单击任何随机用户时删除他。这是我尝试做到这一点的第二天。
我做了这样的事情:
$id = $user->id;
$sql = "DELETE FROM `users` WHERE `id` = :id";
$query = $handler->prepare($sql);
$query->execute(array(":id" => $id));
但这不是一个解决方案,这将删除所有加入该页面的人。我想在单击“删除”按钮时删除该按钮所在的用户。希望您能理解我,我会向您展示我想要的图片。
最佳答案
您应该使用 $_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/
我是一名优秀的程序员,十分优秀!