hi all.
I have some problem. I want to make Delete multiple rows in mysql with check box.
so I did it below.
but delete doesn't work. could you tell me where is wrong ?

<?php
include'conn.php';
mysql_select_db("auction")or die("cannot select DB");

$sql="SELECT * FROM family";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>



<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>relationship</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>tel</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>addr</strong></td>
</tr>

<?php

while($row = mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['relationship']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $row['relationship']; ?></td>
<td bgcolor="#FFFFFF"><? echo $row['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $row['tel']; ?></td>
<td bgcolor="#FFFFFF"><? echo $row['addr']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php

// Check if delete button active, start this 
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM family WHERE relationship='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php 
if($result){
echo "<script>
    alert('delete')
    location.href = 'fam_list.php'
    </script>";


    }
}
mysql_close();
?>

</table>
</form>
</td>
</tr>
</table>

Recommended Answers

All 2 Replies

Member Avatar for LastMitch

@eulo

You example looks familiar, I think I have used that example before just a few months ago. It should work.

Here is the code that looks similiar to yours:

http://www.phpeasystep.com/mysql/8.html

Just double check each code in each line.

Plus double check your DB too

You have this

for($i=0;$i<$count;$i++){

But don't seem to have set a value for $count anywhere so the is no value to compare $i to.

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.