Home  >  Article  >  Backend Development  >  php batch delete operation

php batch delete operation

高洛峰
高洛峰Original
2017-02-28 15:08:344267browse

Deleting multiple records in batches is very troublesome if there is no batch deletion function for relatively large amounts of information.

1. Take a table from the database and write a check box to select

You can add a select all check box

Connect to the database or something Don’t even write it

Code:

<form action="piliangshanchu.php" method="post" >
<table border="1" cellspacing="0" cellpadding="0">
  <tr>

    <td width="200">
      <input type="checkbox" value="&#39;&#39;" name="dx" onclick="checkall(this)" />
      编号</td>
    <td width="200">姓名</td>
    <td width="200">电话</td>
    <td width="200" >分组</td>
    <td width="200" >操作</td>
  </tr>
<tr>
    <td>
    <input type=&#39;checkbox&#39; value=&#39;{$attr[0]}&#39; name=&#39;item[]&#39; class=&#39;ck&#39; />
    {$attr[0]}</td> 

    <td>{$str}</td>
    <td>{$attr[2]}</td>
    <td>{$nation}</td>

</tr>


</table>

  <input type="submit" value="批量删除"/>
  </form>

plus a batch delete button

Picture above:

php batch delete operation

If I click to select all, I can easily select all by using js click event

Code:

<script>
  function xxx(qx)
  {

//全选多选的选中状态
    var ck = document.getElementsByClassName("ck");
 //让下面所有的多选选中状态改变
    if(qx.checked)
    {
      for(i = 0;i < ck.length ; i++)
      {
        ck[i].setAttribute("checked","checked");
//状态改变为选中
      }
    }
    else
    {
      for(var i = 0;i < ck.length;i++)
      {
        ck[i].removeAttribute("checked");
//移除选中
      }
    }
  }
</script>

2. Deletion processing page

Code:

<?php
$arr = $_POST["item"];
$db = new mysqli("localhost","root","12345678","heiheihei");
//foreach($arr as $v)
//{
//  $sql = "delete from contacts WHERE id=&#39;{$v}&#39;";
//  $db->query($sql);
//}
$str = implode("&#39;,&#39;",$arr);//拼接字符,
$sql = "delete from contacts WHERE id in(&#39;{$str}&#39;)";
//2&#39;,&#39;8&#39;,&#39;4
if($db->query($sql))//判断是否查询成功,
{
  header("location:shouye.php");
  //成功就跳转
}



?>

The data transmission using foreach is too slow, and the deletion traversal is too many, so it is judged directly ;

For more articles related to PHP batch deletion operations, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn