dtujfmfs06058 2016-07-27 19:22
浏览 59
已采纳

如何使用Checkbox删除多个条目? (PHP)

So i've been looking at a few resources on using multiple checkboxes as input. I'm building a PM system and want the user to be able to check off any message and then delete them with a button.

The problem is that whenever I check off any message(s):

A: You have to click the delete button twice.

B: Only the first message ever in the list ever gets deleted.

The button group:

<form method="post"
    <div class="button-group">
        <button type="submit" name="delete" class="delete button inbox">Delete</button>
        <button type="submit" name="mark" class="mark button inbox">Mark as Read</button>
    </div>

Later on the table rows:

<?php
    while ($row = mysqli_fetch_array($result)){?>
        <tr>
            <td><?php echo $row['PM_User1ID'];?></td>
            <td><?php echo $row['PM_User2ID'];?></td>
            <td><?php echo $row['PM_Subject'];?></td>
            <td><?php echo $row['PM_Timestamp'];?></td>
            <td>
                <label>
                    <input type="checkbox" name="check_list[]" value="<?php echo $row['PMID'];?>"><?php echo $row['PMID'];?>
                </label>
                </form>
            </td>
            <?php echo '<td><a name="'.$row['PMID'].'"href = "?link='.$row['PMID'].'">Reply</a></td>'; ?>
        </tr>
    <?php } ?>

And finally the Submit Post:

<?php
        if (isset ($_POST['delete'])){

            if(!empty($_POST['check_list'])){

                foreach($_POST['check_list'] as $selected){

                    $pmDelete = "DELETE FROM pm WHERE PMID = $selected"; 
                    $deletePM = $con->query($pmDelete);

                }
            }   
        }
    ?>

PM Page

GIF of issue

  • 写回答

1条回答 默认 最新

  • duanjiaoxi4928 2016-07-27 21:20
    关注

    As it turns out (from conversing with the OP in comments), is that the empty() function for the checkboxes needed to be isset().

    Then a (header) redirection was needed (upon successful deletion) in order to show the (new) results.

    References:

    You should also look into using a prepared statement:


    Footnotes:

    As I also mentioned, <form> cannot be child of <table> and the missing > for <form method="post" which would cause havoc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan