PHP 使用 while 循环从数据库中删除项目



我有一个while循环,循环所有从数据库中撤回。问题是在仪表板上它有一个接受和拒绝选项,对于拒绝,我将确认从0更改为1。但是在 while 循环(单击时(它对所有条目执行,因此它将所有条目更改为1,而不仅仅是提供的id

$get_iteme = $connect->query(
"SELECT id,username,pmacc,quantity,datew
FROM withdraws 
WHERE confirmed = 0 ORDER BY id
");
$row_depo = $get_iteme->num_rows;
if($row_depo) {
while($item_fetch = $get_iteme->fetch_array(MYSQLI_ASSOC)) {
$id = $item_fetch['id'];
echo '
<div class="comment-body">
<div class="user-img">
<img src="modules/dashboard/plugins/images/users/pawandeep.jpg" alt="user" class="img-circle">
</div>
<div class="mail-contnet">
<h5>Test user</h5><span class="time">'.$item_fetch['datew'].'</span>
<br/><span class="mail-desc">PERFECT MONEY ACCOUNT : '.$item_fetch['pmacc'].'</span>
<form method="POST"><input type="submit" name="accept" value="Accept" class="btn btn btn-rounded btn-default btn-outline m-r-5">
<input type="submit" name="decline" value="Decline" class="btn-rounded btn btn-default btn-outline">
</form>
</div>
</div>
';
if(isset($_POST['accept'])) {
} else if(isset($_POST['decline'])) {
$connect->query("UPDATE withdraws SET confirmed = 1 WHERE id = '$id' ") or die(mysqli_error($connect));
}
}
}

$_POST['decline'] 将被发送一次,当执行的 PHP 代码将其作为一个变量读取时,您需要将 $_POST['decline'] 选定的项目添加为数组,

例: 在表单页面中,复选框如下所示:

<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="<?=$rowid?>">
<input type="checkbox" name="check_list[]" value="<?=$rowid?>">
<input type="checkbox" name="check_list[]" value="<?=$rowid?>">
<input type="checkbox" name="check_list[]" value="<?=$rowid?>">
<input type="checkbox" name="check_list[]" value="<?=$rowid?>">
<input type="submit" /> 

在测试中.php它会像这样:

foreach($_POST['check_list'] as $item){ // query to delete where item = $item }

您必须在<form>标签内发送带有隐藏字段的条目 id,以便知道应该接受或拒绝哪个条目。所以表单应该看起来像这样:

<form method="POST">
<input type="hidden" name="entryId" value="the_id_of_your_entry_here" />
<input type="submit" name="accept" value="Accept" class="...">
<input type="submit" name="decline" value="Decline" class="...">
</form>

然后,您可以检查entryId并做出相应的反应,如下所示:

if (isset($_POST['entryId'], $_POST['accept'])) {
$stmt = $connect->prepare('UPDATE withdraws SET confirmed = 1 WHERE id = ?');
$stmt->execute(new array($_POST['entryId'])); // or whatever API for prepared statements you are using...
$stmt->close();
}

你把这个代码块放在 while 循环之外/之前。

你的查询应该是这样的;

$connect->query("UPDATE withdraws SET confirmed = 1 WHERE id = $id ") or die(mysqli_error($connect));

删除 ID 两边的引号

最新更新