PHP:删除选中的行

时间:2013-09-16 22:22:22

标签: php mysql checkbox

我首先要提到的是,我已经搜索了Google和stackoverflow以及其他任何地方,并尝试使用论坛中提供的脚本并编写我自己的脚本,但没有任何对我有用。我完全陷入困境。

所以,我尝试做的就是编写一个脚本,从MySQL表中删除已检查的行。这是我在PHP文件中编写的HTML:

<tr class="noP">
  <td class="check"><input class="checkbox" name="checkbox[]" type="checkbox" value="'.$row["PID"].'"/></td>
  <td class="id">'.$row['PID'].'</th>
  <td>'.$row["name"].'</th>
  <td>'.$row["surname"].'</th>
  <td>'.$row["pcode"].'</th>
  <td class="address">'.$row["address"].'</th>
  <td class="email">'.$row["email"].'</th>
  <td>'.$row["phone"].'</th>
  <td class="education">'.$row["education"].'</th>
  <td class="remarks">'.$row["remarks"].'</th>
</tr>

这里是$row = mysql_fetch_assoc($qParts);,所以这个数组只是MySQL DB中字段值的收集器。

基本上,我尝试做的只是一张表,其中列出的所有参与者都能够删除所选择的参与者。

我非常感谢您提供的任何帮助。谢谢!

2 个答案:

答案 0 :(得分:2)

这可以帮到你:

foreach($_REQUEST['checkbox'] as $val)
    $delIds = intval($val);

$delSql = implode($delIds, ",");

mysql_query("DELETE FROM table WHERE PID IN ($delSql)");

因此,从$_GET / $_POST获取输入数组,对其进行清理(稍微),然后将其删除以获取ID列表(例如5,7,9,13)。然后将其提供给SQL语句,使用IN运算符匹配这些ID。

请注意,您应该使用预准备语句或类似语句来执行此操作。已经有一段时间了,所以我不能把它们写在手边,但这应该给你一个要点。

要使用PDO执行此操作,请查看此处。它有点复杂,因为你需要动态创建占位符,但它应该工作相同。

Reference - frequently asked questions about PDO

答案 1 :(得分:0)

我想我可以帮助你。在我的学期项目中,我遇到了同样的问题。只用HTML和PHP就可以解决这个问题。

我假设PID是表中的主键。这里的技巧是将整个表放在一个表单中,使它看起来像这样:

      <form action="/*NAME OF THIS PAGE HERE*/.php" method="POST">

                       <?php

     if(isset($_POST['delete']))   //THE NAME OF THE BUTTON IS delete. 
    {
       foreach ($_POST["checkbox"] as $id){
       $de1 = "DELETE FROM //table-name WHERE PID='$id'";
       if(mysqli_query($conn, $de1))
       echo "<b>Deletion Successful. </b>";
       else
       echo "ERROR: Could not execute";
       }
    }
        if(isset($_POST['delete'])){
            echo"<b>Please make a selection to complete this operation.</b>";
        }

    ?>
    </br>


<!-- below you will see that I had placed an image as the delete button and stated its styles -->

                              <button type="submit" name="delete" value="delete" style="float:right;border:none; background:none;">
                              <img style="width:55px; height:55px;margin-left:10px; margin-bottom:6px; float:left;" src="images/del.png">
                              </button>


                                    <table>
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>PID</th>
                                        <th>NAME</th>
                                        <th>SURNAME</th>
                                        <!--REST OF THE TABLE HEADINGS HERE -->
                                    </tr>
                                    <?php $fqn="SELECT * FROM //table-name here;
                                    $fqn_run=mysqli_query($conn,$fqn);
                                    while($row=mysqli_fetch_array($fqn_run)):?>
                                </thead>
                                <tbody>
                                    <tr class="noP">
  <td class="check"><input class="checkbox" name="checkbox[]" type="checkbox" value="<?php echo $row["PID"];?>"></td>
  <td><?php echo $row['PID'];?></td>
  <td><?php echo $row["name"];?></td>
  <td><?php echo $row["surname"];?></td>
  <!-- REST OF THE ROWS HERE -->
</tr><?php endwhile;?>
                                </tbody>
                            </table>
    </div>
    </div>      

    </form>

希望这会对你有所帮助。