PHP Bulk Delete operations

Source: Internet
Author: User
Bulk deletion of multiple records, for more information, if there is no bulk deletion feature is very cumbersome.

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

You can add a Select all check box

Connect to the database, not even write anything.

Code:

<form action= "piliangshanchu.php" method= "POST" ><table border= "1" cellspacing= "0" cellpadding= "0" >  <tr>    <td width= "$" >      <input type= "checkbox" value= "" "Name=" DX "onclick=" Checkall (This) "/ >      number </td>    <td width= "$" > name </td>    <td width= "$" > Phone </td>    < TD width= > Group </td>    <td width= "$" > Action </td>  </tr><tr> <td>    <input type= ' checkbox ' value= ' {$attr [0]} ' name= ' item[] ' class= ' ck '/>    {$attr [0]}</td>     <td>{$str}</td>    <td>{$attr [2]}</td>    <td>{$ nation}</td></tr></table>  <input type= "Submit" value= "bulk Delete"/>  </form>

Plus a bulk delete button

If I click the Select all, the use of JS Click event can be easily implemented by the full selection

Code:

<script>  function xxx (QX)  {//Select all selected multiple selection    var ck = document.getelementsbyclassname ("ck"); Let all of the following multiple select states change    if (qx.checked)    {for      (i = 0;i < ck.length; i++)      {        Ck[i].setattribute ("Checked" , "checked");//Change status to checked      }    }    else {for      (var i = 0;i < ck.length;i++)      {        ck[i]. RemoveAttribute ("checked");//Remove selected      }    }  }</script>

2. Deleted processing page

Code:

<?php$arr = $_post["item"]; $db = new mysqli ("localhost", "root", "12345678", "Heiheihei"),//foreach ($arr as $v)//{//< c18/> $sql = "Delete from contacts WHERE id= ' {$v} '";//  $db->query ($sql);/} $STR = Implode ("', '", $arr);//stitching character, $ sql = "Delete from contacts WHERE ID in (' {$str} ')";//2 ', ' 8 ', ' 4if ($db->query ($sql))//Determine if query succeeded, {  header (" Location:shouye.php ");  Success on Jump}?>

With foreach data transmission too slow, delete traverse a large variety, so direct judgment;

Related Article

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.