【问题标题】:PHP Delete an Item from the DB using a while loopPHP使用while循环从数据库中删除一个项目
【发布时间】:2017-07-29 11:35:58
【问题描述】:

我有一个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));
        }
    }
}

【问题讨论】:

  • PHP 从 while 循环中删除一个 db 项?你的问题是什么,你的头衔是什么?

标签: php mysql database mysqli


【解决方案1】:

$_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 }

【讨论】:

    【解决方案2】:

    您必须在 &lt;form&gt; 标记内发送带有隐藏字段的条目 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 循环之外/之前。

    【讨论】:

    • 谢谢!这解决了我的问题,我完全忘记了我需要使用表单中的隐藏输入来使用 while 循环的外部函数
    【解决方案3】:

    您的查询应该是这样的;

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

    删除 id 周围的引号

    【讨论】:

    • 你如何检查。你先把所有东西都改成0
    猜你喜欢
    • 2013-04-22
    • 2013-07-15
    • 1970-01-01
    • 2018-05-06
    • 2011-03-09
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多