johne90 Posted February 22, 2008 Share Posted February 22, 2008 I have been stuck on why this won't work for a while now. It doesn't give any errors, it seems like everything is fine, it just doesn't delete anything. if ( isset($_POST['remove_selected']) ) { $postCount = count($_POST['id']); for ( $i=0; $i < $postCount; $i++ ) { $deleteIds .= $_POST['id'][$i].','; } $deleteIds = rtrim($deleteIds,','); mysql_query('delete from items where id in ('.$deleteIds.')'); } <form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <ul id="container" class="codeBoxes"> <? echo "<li><input class=\"checkbox\" type=\"checkbox\" value=\"$id\" name=\"id[]\"></li>"; ?> </ul> </td> </tr> <tr> <td> <input type="Submit" name="remove_selected" value="Remove Selected"> </td> </tr> </table> </form> If anyone knows whats wrong I would really appreciate the help. Link to comment https://forums.phpfreaks.com/topic/92463-dynamic-checkbox-deletion-not-working/ Share on other sites More sharing options...
PHP Monkeh Posted February 22, 2008 Share Posted February 22, 2008 Try: mysql_query("delete from items where id = '".$deleteIds."') or die(mysql_error()); Adding the mysql_error() on to the end will let you know what error is being generated. Link to comment https://forums.phpfreaks.com/topic/92463-dynamic-checkbox-deletion-not-working/#findComment-473717 Share on other sites More sharing options...
johne90 Posted February 22, 2008 Author Share Posted February 22, 2008 Okay, it returned: Unknown column '01' in 'where clause' 01 being the $id that was passed. Link to comment https://forums.phpfreaks.com/topic/92463-dynamic-checkbox-deletion-not-working/#findComment-473730 Share on other sites More sharing options...
drisate Posted February 22, 2008 Share Posted February 22, 2008 try something like foreach($_POST['remove_selected'] as $value){ //MySQL delete using $value as id } Link to comment https://forums.phpfreaks.com/topic/92463-dynamic-checkbox-deletion-not-working/#findComment-473735 Share on other sites More sharing options...
johne90 Posted February 22, 2008 Author Share Posted February 22, 2008 try something like foreach($_POST['remove_selected'] as $value){ //MySQL delete using $value as id } Yeah, but won't that run a different query for each item being deleted? I was trying to avoid that. Link to comment https://forums.phpfreaks.com/topic/92463-dynamic-checkbox-deletion-not-working/#findComment-473739 Share on other sites More sharing options...
drisate Posted February 22, 2008 Share Posted February 22, 2008 Not nessesarly you can foreach($_POST['remove_selected'] as $value){ $del_string="id='$value' or".$del_string; } $del_string = substr($del_string, 0, -3); Then you DELETE using $del_string in your query Link to comment https://forums.phpfreaks.com/topic/92463-dynamic-checkbox-deletion-not-working/#findComment-473742 Share on other sites More sharing options...
johne90 Posted February 22, 2008 Author Share Posted February 22, 2008 Oh. Thanks for the help. With that mysql_error() I ran a search and found the solution. Problem: mysql_query('delete from items where id in ('.$deleteIds.')'); Solution: mysql_query("delete from items where id in ('$deleteIds')"); Link to comment https://forums.phpfreaks.com/topic/92463-dynamic-checkbox-deletion-not-working/#findComment-473753 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.