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'>&nbsp;</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);
?>

Recommended Answers

All 5 Replies

Hi

You have declare $count in line 63, where is the value of count.

So try to use below line after 62.

$count = count($checkbox); 

I tried this.
but it still not working

can you replace 62 to 69

$checkbox=$_POST['checkbox'];
    foreach($checkbox as $a => $b)
    {
        echo $del_id = $checkbox[$a];

        $sql="DELETE FROM contact WHERE id='$del_id'";
        $result = mysql_query($sql);

    }

many thanks
it works now

mark it solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.