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

标签 php mysql database mysqli

我有一个 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" /> 

在 test.php 中它会是这样的:

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

关于PHP 使用 while 循环从数据库中删除一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45388805/

相关文章:

mysql - 从表中选择行并从其他表中选择其他数据(如果存在)

sql - 为什么仅索引扫描需要这么长时间?

php - PHPUnit 的问题 (Linux) - PHP fatal error

mysql - 计算 SQL 查询中的总行数

javascript - 如何更改html文件类型属性中 "no file selected"按钮旁边的文本 "Browse"?

mysql - lat lng自动从固定位置到数据库中的lan lng距离(澳大利亚)

php - 运行 migrate :install on Uniform WAMP server 时 Laravel 4.1 中带有 mysql 驱动程序的 PDOException

java - neo4j 中的数据模型

php - 学说预期 Lexer::T_FROM,得到 '>'

php - 将 CSS 类分配给第一个和第三个 html 元素