Rocketaka Posted March 1, 2007 Share Posted March 1, 2007 Sorry if this has already been solved but none of the topics on this have been able to fix my problem. I've got a software script that stores software titles. I have a page that displays all of the software titles and gives me the ability to delete software from the database. The problem is I want to be able to use checkboxes and it's just not working. So far the best I have gotten is the form will delete the software title that's the last title in the while loop. Here's my code: <form method='POST' action='index.php?cat=admin&page=removesoftware' style='margin:0px;'>"; while ($data = mysql_fetch_array($query)) { if($class == $class1) { $class = $class2; } else { $class = $class1; } $id = $data['id']; $title = $data['title']; $category = $data['category']; $status = $data['status']; $date = $data['date']; $by = $data['by']; echo "<tr class='$class'> <td width='55%'> <input type='checkbox' name='checked[]' value='".$id."'> <input type='hidden' name='id' value='$id'> <input type='hidden' name='title' value='$title'> ".$data['title']."</td> <td width='25%'>".$data['category']."</td> <td width='20%'>".$data['status']."</td></tr>"; } echo "</table><br>"; echo " <input type='submit' name='remove' style='border:1px #1469A2 solid; margin-left:10px;' value='Remove Selected'> </form> "; Here's the code when the button is pressed: if(isset($_POST['remove'])) { foreach ($_POST['checked'] as $val) { $id = $_POST['id']; $title = $_POST['title']; $sql = "DELETE FROM `software` WHERE `id` = '$id'"; mysql_query("$sql") or die (mysql_error()); echo "You have removed <b>".$title."</b> successfully!\n"; } echo "<a href='index.php?cat=admin&page=software'>Go Back?</a>"; } Link to comment https://forums.phpfreaks.com/topic/40776-solved-while-foreach-problem/ Share on other sites More sharing options...
bwochinski Posted March 1, 2007 Share Posted March 1, 2007 How about: if ( isset($_POST['remove']) AND sizeof($_POST['checked']) ) { $ids = implode(',',$_POST['checked']); foreach ( $_POST['checked'] as $tmp ) { $title_list[] = $_POST['title'][$tmp]; } $titles = implode(', ',$title_list); $sql = "DELETE FROM `software` WHERE `id` IN ($ids)"; mysql_query("$sql") or die (mysql_error()); echo "You have removed <b>".$titles."</b> successfully!\n"; } echo "<a href='index.php?cat=admin&page=software'>Go Back?</a>"; That way it's all done in one query. And update the form to include the title array: echo "<tr class='$class'> <td width='55%'> <input type='checkbox' name='checked[]' value='".$id."'> <input type='hidden' name='title[$id]' value='$title'> ".$data['title']."</td> <td width='25%'>".$data['category']."</td> <td width='20%'>".$data['status']."</td></tr>"; Link to comment https://forums.phpfreaks.com/topic/40776-solved-while-foreach-problem/#findComment-197424 Share on other sites More sharing options...
Rocketaka Posted March 2, 2007 Author Share Posted March 2, 2007 Thank you very much. It worked Link to comment https://forums.phpfreaks.com/topic/40776-solved-while-foreach-problem/#findComment-197986 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.