PHP Bulk Delete allows multiple or all data to be deleted together
The new php file displays the contents of the database:
<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" ><tr> <td><input type= " CheckBox "id=" QX "onclick=" Xuanzhong () "/> Select all </td> <td> code </td> <td> name </td > </tr> <?phpinclude ("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 a class file for database queries:
<?phpclass dbda{public $host = "localhost";p ublic $uid = "root";p ublic $pwd = "";p ublic $dbname = "12345";//Member Method public fu Nction 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 Select check box to the table:
<td><input type= "checkbox" id= "QX" onclick= "Xuanzhong ()"/> Select all </td>
<td><input type= ' checkbox ' name= ' ck[] ' class= ' ck ' value= ' {$v [0]} '/></td>
Show:
Use the JS control check box to select All and deselect all:
<script type= "Text/javascript" >function Xuanzhong () {//Take the Select state of the All-selected button var zt = document.getElementById ("QX"). checked;//let all of the checkboxes selected below change 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 and submit button on the outside of the table, and use JS to control click Delete to display detailed prompt information complete PHP code:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Finally, the new delete processing PHP file;
<?php$ck = $_post["ck"];include ("DBDA.class.php"), $db = new Dbda ();//The First way/*foreach ($ck as $v) {$sql = "Delete from NAT Ion where code= ' {$v} ' "; $db->query ($sql, 0);} *///the second way//in (', ', ', ', ', ') $str = Implode ("', '", $ck); $str = "(' {$str} ')"; $sql = "Delete from Nation where code in {$str}"; $db->query ($sql, 0); Header ("location:main.php");
Click OK:
Bulk Delete succeeded!
PHP Bulk Delete