PHP batch delete data method analysis

  • 2020-03-31 16:46:40
  • OfStack

You can refer to the following article (link: #)
SQL: $SQL="delete from 'doing' where id in ('1,2,3,4')";
Data is separated by commas.
Form:

  <form action="?action=doing" method="post"> 
  <input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="1"/> 
  <input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="2"/> 
  <input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="3"/> 
  <input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="4"/> 
  <input type="submit"/> 
  </form> 

So $ID_Dele=$_POST['ID_Dele'] will be an array, and while PHP is weakly typed, it's not as weak as ASP.
ASP can directly:
SQL="delete from [doing] where id in ('"&ID_Dele&"')" to delete. But PHP can't put $ID_Dele directly in there. Because $ID_Dele is not '1,2,3,4', because $ID_Dele is an array with keys and values.
Okay, it's not hard in PHP either. There's a function called implode(), that's right. A function that is the opposite of a splitter () that is split by a character (such as a comma), whereas a splitter () can be spliced as a string.
Therefore:
 
  $ID_Dele= implode(",",$_POST['ID_Dele']); 
  $SQL="delete from `doing` where id in ($ID_Dele)"; 


This site provides test code:

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<?php 
if ($_POST["action"]="doing"){ 
$del_id=$_POST["ID_Dele"]; 
$ID_Dele= implode(",",$_POST['ID_Dele']); 
echo " After the merger :".$ID_Dele."<br /> Before the merger :"; 
if($del_id!=""){ 
$del_num=count($del_id); 
for($i=0;$i<$del_num;$i++){ 
echo $del_id[$i]; 
} 
} 
}else{ 
echo " Please submit "; 
} 

?> 
<form action="?action=doing" method="post"> 
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value=" The first 1 a "/> The first 1 a  
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value=" The first 2 a "/> The first 2 a  
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value=" The first 3 a "/> The first 3 a  
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value=" The first 4 a "/> The first 4 a  
<input type="submit"/> 
</form> 

Related articles: