PHP USES a checkbox to delete multiple records at once
- 2020-03-31 20:25:36
- OfStack
A simple example
There is an existing database of student information and multiple records need to be deleted at once
Create a file called del.php
The code is as follows:
<form action="sc.php" method=post>
<table border=1 width=60% align=center>
<caption> Student information sheet </caption>
<th> options </th><th> Student id </th><th> The name < /th><th> gender </th><th> The phone </th>
<?php
$link=mysql_connect("localhost","root","");
mysql_select_db("zs");
$exec="select * from student";
$result=mysql_query($exec);
while($rs=mysql_fetch_object($result))
{
$id=$rs->sID;
$name=$rs->sName;
$sex=$rs->sSex;
$phone=$rs->sPhone;
?>
<tr>
<td> <input type=checkbox name=de[] value=<?php echo $id?>></td><td><?php echo $id?></td><td><?php echo $name?></td><td><?php echo $sex?></td><td><?php echo $phone?></td>
</tr>
<?php
}
mysql_close();
?>
</table>
<center><input type=submit value=" delete "></center>
</form>
This file is mainly used to display the data in the database and display it.
Create a new file named sc.php with the following code:
<?php
$link=mysql_connect("localhost","root","");
mysql_select_db("zs");
$id=$_POST['de'];
foreach($id as $ide){
$exec="delete from student where sID=$ide";
$result=mysql_query($exec);
if((mysql_affected_rows()==0) or (mysql_affected_rows==-1))
{
echo " No record was found, or an error occurred while deleting ";
exit;
}
else{
echo " Student information has been deleted ";
}
}
mysql_close();
?>
Delete the records separately using foreach.