PHP Bulk Delete

Source: Internet
Author: User

Before we through the PHP operation of the database, to achieve a simple data deletion and modification. Today we continue to work through the database operation of PHP to further improve the relevant design, today we want to implement the bulk deletion function.

Before we have made the query page, now to implement the bulk delete function, first to each code before adding a check box and related action buttons. We have previously shown through the data in the database through the traversal, therefore, for the check box settings, we just need to base on the previous code, a little setup can easily give each code with a check box and add the action button (see Code for details).

<form action= "piliangshanchu.php" method= "POST" >
<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
<tr>
<td> Code </td>
<td> name </td>
<td> Sex </td>
<td> ethnic </td>
<td> Birthdays </td>
<td> Operations </td>
</tr>
<?php
Created objects
$db = new Mysqli ("localhost", "root", "123", "MyDB");
Judging if there is an error
!mysqli_connect_error () or Die ("Connection failed! ");
Write SQL statements
$sql = "SELECT * from Info";
Execute SQL statement
$result = $db->query ($sql);

Reading data
$attr = $result->fetch_all ();

Iterate through the array, showing
foreach ($attr as $v)
{

$sex = $v [2]? " Male ":" Female ";

$sql = "Select Name from Nation where Code = ' {$v [3]} '";
$r = $db->query ($sql);
$a = $r->fetch_row ();

echo "
<tr>
<td>
<input class= ' ck ' name= ' ck[] ' type= ' checkbox ' value= ' {$v [0]} '/>//Add a name here, in bulk Delete, let him commit in the form of an array, so that error can be avoided
{$v [0]}</td>
<td>{$v [1]}</td>
<td>{$sex}</td>
<td>{$a [0]}</td>
<td>{$v [4]}</td>
<td><a href= ' delete.php?code={$v [0]} ' > Delete </a><a href= ' update.php?code={$v [0]} ' > Modify </a ></td>
</tr>
";
}
?>
<tr>
<td><input type= "checkbox" onclick= "Checkall (This)"/> Select all </td>
<td><input type= "Submit" value= "Bulk Delete"/></td>
</tr>
</table>
<a href= "add.php" > Add Data </a>
</form>
</body>
<script type= "Text/javascript" >
function Checkall (a)
{

To fetch the values of all the checkboxes according to the class name
var ck = document.getelementsbyclassname ("ck");

Use for loop to get check box selected state
for (Var i=0;i<ck.length;i++)
{
if (a.checked)
{
Ck[i].setattribute ("Checked", "checked");//Ck[i here] is the value taken to each checkbox
}
Else
{
Ck[i].removeattribute ("checked");
}
}

}
</script>

After setting the check box, the PHP code is executed in the background, so create a new code page that is designed to execute PHP code

<?php
$ck = $_post["ck" The];//array is named CK and is submitted as post, so use the Post method here
Include ("DBDA.class.php");
$db = new Dbda ();

The foreach ($ck as $v)//ck is a one-dimensional array, so each v represents a primary key value
{
$sql = "Delete from Info where code= ' {$v} '";
$db->query ($sql, 0);//execute MySQL statement
}

Header ("location:main.php");

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.