chrisj989 Posted July 6, 2006 Share Posted July 6, 2006 Hey Guys, first post here and I hope you can help me out with somethingI have a checkbox next to each field and what I want to do is allow users to select the rows through the checkboxes and the press an option button to delete the row. The checkboxes are inserted after every field.My code is pretty crummy since this is my first time coding. Any and all help would be great, thanks!I'm using PHP with MS SQL 2000 by the way. Quote Link to comment https://forums.phpfreaks.com/topic/13858-how-do-i-delete-multiple-rows-with-php/ Share on other sites More sharing options...
CamW Posted July 7, 2006 Share Posted July 7, 2006 PHP can interpret data submitted through forms as arrays, so it's pretty easy to treat a series of checkboxes as one array. You just need to add [] to the end of the element name. If we assume you have the ids for all your rows when building your checkbox list, it's pretty easy to generate checkboxes that will pass an array of ids to be deleted on a submit.[code]$ids = getListOfIds();foreach($ids as $id) { echo "<input type=\"checkBox\" name=\"deleteItems[]\" value=\"$id\" /> Row id: $id<br />";}[/code](You can also specify the key name within the brackets, but not specifying is handy here, because PHP will just automatically add the value of a form item to the next index in the array if it isn't specified)Once the user submits that form, you'll have the list of ids the user checked in either $_GET or $_POST, and you can delete accordingly. Let's assume it's in $_POST.[code]foreach($_POST['deleteItems'] as $deleteItem) { executeSQL("DELETE FROM yourTable WHERE id = $deleteItem");}[/code](Of course, executeSQL isn't a real command. You'll need to execute that query using whatever method you use to talk to the database.)Hope that helps!-Cameron Quote Link to comment https://forums.phpfreaks.com/topic/13858-how-do-i-delete-multiple-rows-with-php/#findComment-54430 Share on other sites More sharing options...
chrisj989 Posted July 7, 2006 Author Share Posted July 7, 2006 hmm....ok, this is how each row is setup and displayed...[code] while ($row=mssql_fetch_row($results)) { $fields = mssql_num_fields($results); for( $i = 0; $i < $fields; ++$i){ echo "<td><div align = \"center\">"; echo $row[$i]."</div></td> "; } echo "<td><div align = \"center\"><form><input type=\"checkbox\" id= \"rfs[]\"></div></form></td>"; echo "</tr>"; } [/code]As you can see, the checkboxes are added at the end of each row...but I think they have no relationship to the data, how do I link the checkboxes to the data on the row? Quote Link to comment https://forums.phpfreaks.com/topic/13858-how-do-i-delete-multiple-rows-with-php/#findComment-54457 Share on other sites More sharing options...
CamW Posted July 7, 2006 Share Posted July 7, 2006 You need to set the value attribute of the input tag to be equal to the info about the row you need to delete it. Quote Link to comment https://forums.phpfreaks.com/topic/13858-how-do-i-delete-multiple-rows-with-php/#findComment-54503 Share on other sites More sharing options...
chrisj989 Posted July 10, 2006 Author Share Posted July 10, 2006 so then it would be something like... [code]echo "<td><div align = \"center\"><form><input type=\"checkbox\" id= \"rfs[]\" value = "\$row"\ ></div></form></td>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13858-how-do-i-delete-multiple-rows-with-php/#findComment-55622 Share on other sites More sharing options...
chrisj989 Posted July 11, 2006 Author Share Posted July 11, 2006 hmm....well my form is working now, but I'm not able to actually delete the data, here's the full code, maybe someone can point something out that's blantantly wrong/missing? [code]//Code that puts the SQL query into a string...$query = "SELECT u_fname AS First, u_lname AS Last from tblUsers;"; $query2 = "SELECT u_id AS id FROM tblUsers;"; $results = mssql_query($query); $id = mssql_query($query2);//Code that displays the data in a table...while ($row=mssql_fetch_row($results)) { $fields = mssql_num_fields($results); for( $i = 0; $i < $fields; ++$i){ echo "<td><div align = \"center\">"; echo $row[$i]."</div></td> "; } echo "<td><div align = \"center\"><input type=\"checkbox\" name = \"$id\" value = \"$id\"></div></td>"; echo "</tr>"; //Code that TRIES to delete multiple rows of data... echo "Before If Statement <br />"; //For Debugging purposes if(isset($_POST['deleter'])){ foreach($_POST['Delete'] as $del){ $qrem = "DELETE FROM tblUsers WHERE u_fname = $del[0] AND u_lname = $del[1]"; $execute = mssql_query($qrem); } echo "Deletion Complete!"; //Debug } else echo "Do Nothing!"; //Debug[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13858-how-do-i-delete-multiple-rows-with-php/#findComment-56401 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.