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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 } Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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')"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.