How to Delete Multiple Records in PHP MySQL | Learn MySQL
×


How to Delete Multiple Records

3783

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>



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments