php batch deletion code ideas: 1. Set the class file for database query; 2. Add selection check boxes to the table; 3. Use js to control the selection and deselection of all check boxes; 4. Add a form form and submit button outside the form, and use js to control the detailed prompt information when clicking delete; 5. Create a new php file for deletion processing.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
php What is the idea of deleting codes in batches?
php batch deletion can delete multiple or all data at the same time
Create a new php file to display the content in the database:
<table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td><input type="checkbox" id="qx" onclick="xuanzhong()" />全选</td> <td>代号</td> <td>名称</td> </tr> <?php include("DBDA.class.php"); $db = new DBDA(); $sql = "select areacode,areaname from nation"; $attr = $db->Query($sql); foreach($attr as $v) { echo "<tr> <td><input type='checkbox' name='ck[]' class='ck' value='{$v[0]}' /></td> <td>{$v[0]}</td> <td>{$v[1]}</td> </tr>"; } ?> </table>
DBDA.class.php file is the class file for database query:
<?php class DBDA { public $host="localhost"; public $uid = "root"; public $pwd = ""; public $dbname = "12345"; //成员方法 public function Query($sql,$type=1) { $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname); $r = $db->query($sql); if($type==1) { return $r->fetch_all(); } else { return $r; } } }
Add a selection check box to the table:
<td><input type="checkbox" id="qx" onclick="xuanzhong()" />全选</td>
<td><input type='checkbox' name='ck[]' class='ck' value='{$v[0]}' /></td>
Display:
Use js to control the selection and deselection of all check boxes:
<script type="text/javascript"> function xuanzhong() { //取全选按钮的选中状态 var zt = document.getElementById("qx").checked; //让下面所有的checkbox选中状态改变 var ck = document.getElementsByClassName("ck"); for(var i=0;i<ck.length;i++) { if(zt) { ck[i].setAttribute("checked","checked"); } else { ck[i].removeAttribute("checked"); } } } </script>
Add form form and submit button outside the form, and Use js control to display detailed prompt information when you click delete. Complete php code:
无标题文档
Finally create a new php file for deletion processing;
<?php $ck = $_POST["ck"]; include("DBDA.class.php"); $db = new DBDA(); //第一种方式 /*foreach($ck as $v) { $sql = "delete from nation where code='{$v}'"; $db->Query($sql,0); }*/ //第二种方式 //in ('','','','','') $str = implode("','",$ck); $str = "('{$str}')"; $sql = "delete from nation where code in {$str}"; $db->Query($sql,0); header("location:main.php");
Click OK:
Batch deletion successful!
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the idea of deleting codes in batches in php. For more information, please follow other related articles on the PHP Chinese website!