PHP Bulk Delete

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.