I have a page which displays my database information. Through each loop, there is a checkbox printed that is to delete the associated entry if checked and submitted. The checkbox names are 'delete[]' and there is the hidden value which contains the row id is named 'id[]'.
This is the relevant part of my form:
<tr><td valign='top'>
<label class='amarillo med' style='color:#C00;'>Delete Section </label>
<input type='checkbox' name='delete[]' />
<input type='hidden' name='id[]' value='" . $row['about_id'] . "' />
</td></tr>
This is my php and query
$deleteCount = count($_POST['delete']);
for ($x = 0; $x < $deleteCount; $x++) {
if(isset($_POST['delete'][$x])) {
$sql = 'DELETE FROM about WHERE about_id = \''.$_POST['id'][$x].'\'';
$result = mysql_query($sql);
}
}
This is what happens.
If three rows are returned and I want to delete only the third, I check the third box and click submit. This deletes the first row that was returned. Again, if three rows are returned and I want to delete the first and third, I submit and the first two rows are deleted.
What it looks like is happening is for every checkbox that is checked, that many rows are deleted starting with the first. Any advice would be greatly appreciated.
id[]
is returning .. include how you are generating value for id
Baba 2012-04-05 21:31
The best and much easier way is to use
<input type="checkbox" name="delete[]" value="<?=$row['about_id']?>"/>
and then
foreach($_REQUEST['delete'] as $delID)
{
...
}
Use the following code
<tr><td valign='top'>
<label class='amarillo med' style='color:#C00;'>Delete Section </label>
<input type='checkbox' name='delete[$row['id']]' />
<input type='hidden' name='id[$row['id']]' value='" . $row['about_id'] . "' />
</td></tr>
where $row['id'] is the id of the row