How to Delete Multiple Records

To delete multiple records, first of all, make a connection for database. A simple way to delete multiple records, you can use checkbox.

For this, check the following Example:

<?php

$connect=mysql_connect("hostname","username","password") OR die ("connection failed");
$db=mysql_select_db("database_name",$connect);

?>

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title> Delete Multiple Records </title>
</head>

// PHP MySql Delete Query Code

<?php
if(isset($_POST['delete']))
{
for($i=0; $i<count($_POST['id']); $i++)
{
$delete_id=$_POST['id'][$i];
$sql="DELETE FROM table_name WHERE id='$delete_id'";
$result=mysql_query($sql);
}
if($result)
{
echo "Values have been deleted.";
}
else
{
echo "Can't delete these values.";
}
}
?>

<body>
<form action="" method="post" enctype="multipart/form-data">
<table border="1" cellpadding="3" cellspacing="3">
<tr>
<th> Id </th>
<th> Name </th>
<th> Email </th>
</tr>
<?php
$query=mysql_query("SELECT * FROM table_name");
$rows=mysql_num_rows($query);
while($fetch=mysql_fetch_array($query)){
?>
<tr>
<td> <input type="checkbox" name="id[]" value="<?php echo $fetch['id']; ?>" /> </td>
<td> <?php echo $fetch['name']; ?> </td>
<td> <?php echo $fetch['message']; ?> </td>
</tr>
<?php  }  ?>
<tr>
<td colspan="3" align="center">
<input type="submit" name="delete" value="Submit" />
</td>
</tr>
</table>
</form>

</body>

</html>



Leave a Reply

avatar
  Subscribe  
Notify of