Hello all,
There is my table in database. want to delete multiple rows from MYSQL database. I have created this selectcon.php file to select various links and delete them using checkboxes.
but it doesn't
here is my code
<!DOCTYPE html>
<?php
include("emty.php");
?>
<center>
<?php
include("dbcon.php");
$result = mysql_query("SELECT * FROM contact");
$count=mysql_num_rows($result);
echo "<table border='1'>
<table width='400' border='0' cellspacing='1' cellpadding='0'>
<tr>
<td><form name='form1' method='post' action=''>
<table width='400' border='0' cellpadding='3' cellspacing='1' bgcolor='#CCCCCC'>
<tr>
<td bgcolor='#FFFFFF'> </td>
<td colspan='4' align='center' bgcolor='#FFFFFF'><strong>Messages</strong> </td>
</tr>
<tr>
<td align='center' bgcolor='#FFFFFF'>#</td>
<th align='center' bgcolor='#FFFFFF'> Name </th>
<th align='center' bgcolor='#FFFFFF'> Email </th>
<th align='center' bgcolor='#FFFFFF'> Message </th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
?>
<html>
<td> <input name="checkbox[]" type="checkbox" value="<?php echo $row['id']; ?>">
</td>
</html>
<?php
echo "<td align='center' bgcolor='#FFFFFF'> " . $row['Name'] . " </td>";
echo "<td align='center' bgcolor='#FFFFFF'> " . $row['Email'] . " </td>";
echo "<td align='center' bgcolor='#FFFFFF'> " . $row['Message'] . " </td>";
echo "</tr>";
}
echo "</table>";
?>
<html>
<br>
<tr>
<td colspan="5" align="right" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
</html>
<?php
// Check if delete button active, start this
if(isset($_POST['delete'])){
$checkbox=$_POST['checkbox'];
for($i=1;$i<=$count;$i++){
$del_id = $_POST['checkbox'][$i];
echo $del_id;
$sql="DELETE FROM contact WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=selectcon.php\">";
}
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con);
?>