How to implement batch deletion operations in php

墨辰丷
Release: 2023-03-27 11:36:02
Original
3876 people have browsed it

This article mainly introduces the PHP batch deletion operation, batch deletion page and .delete processing interface in detail. It has certain reference value. Interested friends can refer to it.

The specific content is as follows

1. Batch delete page piliangcaozuo.php

<body>
<form action="shanchu.php" method="post">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td><input type="checkbox" name="qx" onclick="quanxuan(this)"/>代号</td>
    <td>名称</td>   
  </tr>
  <?php
   require"DBDA.class1.php";
   $db = new DBDA();
   $sql = "select * from nation";
   $arr = $db->query($sql);
   foreach($arr as $v)
  {
    echo "<tr>
        <td><input type=&#39;checkbox&#39; name=&#39;ck[]&#39; class=&#39;ck&#39; value=&#39;{$v[0]}&#39;/>{$v[0]}</td>
        <td>{$v[1]}</td>   
       </tr>";
  }
  ?>  
</table>
<input type="submit" value="批量删除" />
</form>
</body>
<script type="text/javascript">
function quanxuan(qx)
{
  var ck=document.getElementsByClassName("ck");
  if(qx.checked)
  {
    for(var 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>
</html>
Copy after login

refers to the encapsulation class DBDA.class1. php

<?php
class DBDA
{
  public $host = "localhost";
  public $uid = "root";
  public $pwd = "123";
  public $dbname = "test_123";
  //执行SQL语句返回相应的结果
  //$sql 要执行的SQL语句
  //$type 代表SQL语句的类型,0代表增删改,1代表查询
  function query($sql,$type=1)
  {
    $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
    
    $result = $db->query($sql);
    
    if($type)
    {
      //如果是查询,显示数据
      return $result->fetch_all();
    }
    else
    {
      //如果是增删改,返回true或者false
      return $result;
    }
  }
}
Copy after login

2. Delete processing interface sanchu.php

<?php
$arr = $_POST["ck"];

require"DBDA.class.php";
$db = new DBDA();
//delete from nation where code in(&#39;n001&#39;,&#39;n002&#39;,&#39;n003&#39;)

$str = implode("&#39;,&#39;",$arr); 
$sql = "delete from nation where code in(&#39;{$str}&#39;)";
/*echo $sql;*/
if($db->query($sql,0))
{
  header("location:piliangcaozuo.php");
}
Copy after login

Related recommendations:

ThinkPHP implements user information query, update and delete functions

mysql creates root ordinary users and modifiesdelete FunctionDetailed explanation

jQuery implements the add and delete function of user information table

The above is the detailed content of How to implement batch deletion operations in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!