I am trying to delete multiple records using check boxes. I’ve loaded each check box with the record ID:
<?php $row_count = 1; do { ?>
<tr <?php if ($row_count%2) { ?>bgcolor="#F4F4F4"<?php } ?>>
<td align="center" ><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row_contactlist['contact_id']; ?>"></td>
<td align="center" ><p><?php echo $row_contactlist['contact_id'];?></p></td>
</tr>
<?php $row_count++; } while ($row_contactlist = mysql_fetch_assoc($contactlist)); ?>
I’m running the delete using the following link:
<a class="addcontact" href="delete.php?mContact=<?php for($i=0;$i<=$row_count;$i++) { $del_id = $checkbox[$i]; } echo $del_id ?>" style="border-bottom:0px" >Delete Contact(s)</a>
that runs delete.php
if (isset($_GET['mContact'])) {
mysql_query("DELETE FROM contacts WHERE contact_id = ".$_GET['mContact']."");
mysql_query("DELETE FROM history WHERE history_contact = ".$_GET['mContact']."");
mysql_query("DELETE FROM notes WHERE note_contact = ".$_GET['mContact']."");
redirect('You have deleted some contacts',"contacts.php");
}
The redirect works i.e. the contacts page reloads with ‘you have deleted some contacts’ and it looks like I get no errors, but none of the contacts are deleted.
UPDATE:
Thanks guys very speedy response. Total newb to PHP so everything is massively appreciated.
So I’ll use a sumbit button not a hyperlink:
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
I believe you’re having a mixup dealing with the selection of the criteria to be deleted.
The link that takes the user to the delete page has nothing to do with the checkboxes in the code.
I believe what you want to do is
checked="checked"<a href..>to a submit buttondelete.phpthe checkboxes will be in an array$_GET['checkboxes'], which you can iterate throughNOW, onto the issues that @JohnP alluded to:
$_POSTfor a little bit more security through obscurityLIMIT 1on each delete (or update) query so you don’t accidentally delete all the rows in your database.UPDATE
If you want to have a single delete link, you would need to add the link inside the
do...whileloop. It appears that you have the link outside that loop and you loop through every result and add them all to themContacttoken. As a single, long string of numbers, your handling script does not have a way of parsing that information without delimiters.