如何删除选中的用户? PDO
How to delete selected user? PDO
我正在使用 PDO,我想从我的成员列表中删除指定的用户。我制作了 "delete" 按钮显示在每个用户身上,我希望当我点击任何随机用户时,删除他。这是我尝试做到这一点的第二天。
我做了这样的东西:
$id = $user->id;
$sql = "DELETE FROM `users` WHERE `id` = :id";
$query = $handler->prepare($sql);
$query->execute(array(":id" => $id));
但这不是解决方案,这将删除所有人加入该页面的原因。我想在单击 "delete" 按钮时删除该按钮所在的用户。我希望你能理解我,我会给你看我想要的照片。
您应该使用 $_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>
process.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));
}
?>
您的代码存在一个问题,即您实际上并未将数据绑定到查询。如果您打算使用 PDO,请使用准备好的语句。
我创建了一个删除基于 WHERE
的行的函数。
<?php
function pdo_delete($MyConnection, $Table, $Where, $bindArray = array(), $Limit = 1) {
$prep = $MyConnection->prepare("DELETE FROM $Table WHERE $Where LIMIT $Limit");
foreach ($bindArray as $key => $value) {
$prep->bindValue(":$key", $value);
}
$prep->execute();
$prep->closeCursor();
}
?>
$MyConnection stores the pdo connection instance
$Table stores the name of the table you want to target
$Where stores the name of the column you want to target
$bindArray Binds the data to the column and selects the rows that match
$Limit is how many rows you want to allow
这里有一个关于如何使用这个函数的例子。
调用函数
pdo_delete();
呼叫您的 PDO 连接
pdo_delete(null); // Replace null with your own pdo connection, in your case $handle
目标 table
pdo_delete(null, 'users');
你要绑定数据到哪里
pdo_delete(null, 'users', 'id = :id');
绑定数据
pdo_delete(null, 'users', 'id = :id', array('id' => $user->id));
如果需要,请添加限制...如果是,请添加另一个逗号并键入允许的行数。
pdo_delete(null, 'users', 'id = :id', array('id' => $user->id), 2); // Allows 2 columns
您可以更改 vars/function_name/etc 的名称。
这应该可以,我将它用于我的网站。
我正在使用 PDO,我想从我的成员列表中删除指定的用户。我制作了 "delete" 按钮显示在每个用户身上,我希望当我点击任何随机用户时,删除他。这是我尝试做到这一点的第二天。
我做了这样的东西:
$id = $user->id;
$sql = "DELETE FROM `users` WHERE `id` = :id";
$query = $handler->prepare($sql);
$query->execute(array(":id" => $id));
但这不是解决方案,这将删除所有人加入该页面的原因。我想在单击 "delete" 按钮时删除该按钮所在的用户。我希望你能理解我,我会给你看我想要的照片。
您应该使用 $_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>
process.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));
}
?>
您的代码存在一个问题,即您实际上并未将数据绑定到查询。如果您打算使用 PDO,请使用准备好的语句。
我创建了一个删除基于 WHERE
的行的函数。
<?php
function pdo_delete($MyConnection, $Table, $Where, $bindArray = array(), $Limit = 1) {
$prep = $MyConnection->prepare("DELETE FROM $Table WHERE $Where LIMIT $Limit");
foreach ($bindArray as $key => $value) {
$prep->bindValue(":$key", $value);
}
$prep->execute();
$prep->closeCursor();
}
?>
$MyConnection stores the pdo connection instance
$Table stores the name of the table you want to target
$Where stores the name of the column you want to target
$bindArray Binds the data to the column and selects the rows that match
$Limit is how many rows you want to allow
这里有一个关于如何使用这个函数的例子。
调用函数
pdo_delete();
呼叫您的 PDO 连接
pdo_delete(null); // Replace null with your own pdo connection, in your case $handle
目标 table
pdo_delete(null, 'users');
你要绑定数据到哪里
pdo_delete(null, 'users', 'id = :id');
绑定数据
pdo_delete(null, 'users', 'id = :id', array('id' => $user->id));
如果需要,请添加限制...如果是,请添加另一个逗号并键入允许的行数。
pdo_delete(null, 'users', 'id = :id', array('id' => $user->id), 2); // Allows 2 columns
您可以更改 vars/function_name/etc 的名称。
这应该可以,我将它用于我的网站。