Home  >  Article  >  Backend Development  >  PHP batch deletion data program code_PHP tutorial

PHP batch deletion data program code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:361015browse

PHP batch deletion data program code

I believe many friends still don’t know how to batch delete unwanted data. To use PHP to batch delete data, we need to combine it with the in condition of mysql. Achieved, I guess everyone will understand how to do it after reading this sentence. Now I will analyze the process of batch deletion of data in detail for friends who need to know.

We delete the required sql syntax

 delete from aaaa where id in (1,2,3) where 1,2,3 is the record we need to delete

So how to do it in php

1. First, on the article list page (list.php), name the multi-select box: "$del_id[]", and the value is the article ID number.

For example (list.php):

The code is as follows


$result=mysql_query("select * from news");
while($rs=mysql_fetch_array($result)){
?>

}
?>

 代码如下  

 
$result=mysql_query("select * from news");
while($rs=mysql_fetch_array($result)){
?> 
 
  }
?> 
 

2、处理页面(del.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(""); 
}else{ 
 echo(""); 

?>

2. Processing page (del.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("");
}else{
echo("");
}
?>

Case analysis:

 代码如下  

for($i=0;$i<$del_num;$i++){
mysql_query("Delete from news where id='$del_id[$i]'");
}

The above batch core code is the form name 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 to get the submitted array and then we traverse each one to delete it. This is the same as our previous statement. In fact, we can improve it

The code is as follows

$ids = implode(',' ,$_POST['del_id']);
$sql ="delete from aaaa where id in($ids)";
mysql_query($Sql);

代码如下

$ids = implode(',',$_POST['del_id']);
$sql ="delete from aaaa where id in($ids)";
mysql_query($Sql);

This can simplify a lot of statements. Of course, the above needs to determine whether the submitted one is an array. Finally, we perform code optimization and deletion of the del.php file

The code is as follows

if(is_array($del_id)){

$ids = implode(',',$_POST['del_id']);
$sql ="delete from aaaa where id in($ids)";
if( mysql_query($Sql))
{
echo 'Delete successfully';
}
else
{
echo 'Deletion failed';
}
}

代码如下

if(is_array($del_id)){

$ids = implode(',',$_POST['del_id']);
$sql ="delete from aaaa where id in($ids)";
if( mysql_query($Sql))
{
echo '删除成功';
}
else
{
echo '删除失败';
}
}


}
?>


}
?>

http://www.bkjia.com/PHPjc/873236.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/873236.htmlTechArticlePHP batch deletion data program code I believe many friends still don’t know how to batch delete unwanted data, just use php To delete data in batches, we need to combine it with the in condition of mysql...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn