Dragen Posted April 23, 2007 Share Posted April 23, 2007 Okay, I've got this code to create a checkbox... <?php echo "<input type=\"checkbox\" name=\"id\" value=\"" . $row['id'] . "\" />" ?> On the form I usually end up with at least two or three checkboxes using a a while statement to read from a database. The only difference is the value, which are all numerical. When the form gets processed it goes through this code: $sql = "DELETE FROM " . $type . " WHERE id = '" . $_POST['id'] . "'"; Which gets the id from the checkbox and deletes that row in the table. The problem is if I select more than one checkbox it only deletes the last one and not the others... I presume I'd need a foreach statement to go through all the checked ones, but I'm not sure how to do so. If anyone can help? Thanks Link to comment https://forums.phpfreaks.com/topic/48355-solved-mysql-read-from-checkboxes-foreach/ Share on other sites More sharing options...
Barand Posted April 23, 2007 Share Posted April 23, 2007 You need <?php echo "<input type=\"checkbox\" name=\"id[]\" value=\"" . $row['id'] . "\" />" ?> NOTE: name=id[] To process $idlist = join (',' , $_POST['id']); // eg gives "1,3,7" mysql_query ("DELETE FROM table WHERE id IN ($idlist)"); Link to comment https://forums.phpfreaks.com/topic/48355-solved-mysql-read-from-checkboxes-foreach/#findComment-236421 Share on other sites More sharing options...
Dragen Posted April 23, 2007 Author Share Posted April 23, 2007 ah.. I thought I might have to set the name as an array... and using the join is good! I didn't think of that although I'm using implode instead.. purely because I'll remember it easier. Thanks! Link to comment https://forums.phpfreaks.com/topic/48355-solved-mysql-read-from-checkboxes-foreach/#findComment-236423 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.