PHP Bulk Delete Data program code

Source: Internet
Author: User
Tags mysql in php file

I believe many friends do not know how to bulk delete the data, single use PHP bulk delete data We need to combine MySQL in the condition to achieve, this estimate everyone read this sentence to understand how to do, below I will give a friend need to know detailed analysis of the bulk delete data process.

We delete the required SQL syntax

Delete from AAAA where ID in (1,2,3) This 1,2,3 is the record we need to delete.

So how do you do it in PHP?

1, first in the article List page (list.php), will select the basket named: "$del _id[]", the value is the article ID number.

For example (list.php):

  code is as follows &nbs P;

<form name= "Del_form" action= "del.php" method= "POST" > < br> <?php 
$result =mysql_query ("select * from News"),  
while ($rs =mysql_fetch_array ($ Result)) { 
 ?> 
<input name= "del_id[]" type= "checkbox" id= "del_id[" "value=" <?= $rs [ Id]?> "/><?= $rs [title]?> 
 <?php 

 ?> 
  </form>

2, processing page (del.php):

<?php 
 if ($del _id!= "") { 
  $del _num= Count ($del _id);  
 for ($i =0; $i < $del _num; $i + +) { 
 mysql_query ("Delete from News where Id= ' $del _id[$i] ';  
 }
 echo ("<script type= ' Text/javascript" >alert (' Delete succeeded! '); History.back ();</script> ");  
}else{ 
 echo (" <script type= ' text/javascript ' >alert (' Please select the item first! '); History.back ();</script> ");  

Case study:

The bulk core code above is table Single-name for del_id[] and

The code is as follows

for ($i =0; $i < $del _num; $i + +) {
mysql_query ("Delete from News where id= ' $del _id[$i]");
}

This is getting the submitted array, and then we're going to go through a strip of it. It's a little smaller than our previous statement, but we can actually improve

The code is as follows

$ids = Implode (', ', $_post[' del_id '));
$sql = "Delete from AAAA where ID in ($ids)";
mysql_query ($SQL);

So you can Jane a lot of statements oh, of course, the above need to judge the submission over is not an array, and finally we have the del.php file code optimization delete operation

  code is as follows &nbs P;

<?php 
 if (Is_array ($del _id)) { 
  $ids = Implode (', ', $_post[' del_id ']);
$sql = "Delete from AAAA where ID in ($ids)";
 if (mysql_query ($SQL))
{
echo ' delete succeeded ';
}
Else
{
echo ' delete failed ';
}
}



Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.