Mysql Checkbox Delete, Trying to use checkboxes to delete rows in MySQL |
||
Welcome to the Zymic webmaster forums. Our forums are here to provide people the free ability to discuss a range of websites related topics such as design, development coding and marketing.
In order to post you will need to register for a zymic account or if you already have one simply login by using the form on the left.
Zymic Webmaster Forums Web Design & Development Server Side Scripting PHP |
||
![]() |
Mysql Checkbox Delete, Trying to use checkboxes to delete rows in MySQL |
||
Oct 9 2009, 11:24 PM
Post
#1
|
|
|
Newbie ![]() Group: Members Posts: 11 Joined: 28-September 09 Member No.: 111,868 |
I'm trying to make a page that will read in the rows of a table in my database and display a checkbox next to it that will allow the user to select how ever many rows they want and delete them all at once. When they hit delete, it posts back to itself and show's the updated table again.
My first problem is that zymic doesn't seem to recognize my box[] checkbox when it comes time to delete. I'm getting the error: Notice: Undefined index: box in /www/99k.org/c/o/g/cogs/htdocs/test/delete_form.php on line 33 This works fine on my local server, so I'm not sure why it's not working on zymic. My second problem is that it deletes the row just fine, but doesn't update the displayed list until you press the delete button again. Also, when I click a check box and then click the delete button, that error goes away, but the table doesn't update until I press delete again. Here's my code. Any help would be greatly appreciated. CODE <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title></title> </head> <body> <?php include("database_connection.php"); $sql="SELECT * FROM logins"; $result=mysql_query($sql); echo "<form method=post action=''>"; echo "<table border='0' cellspacing='0' style='border-collapse: collapse' width='100' >"; while($rows=mysql_fetch_array($result)){ echo"<tr bgcolor='#ffffff'>"; echo"<td width='25%'>"; echo"<input type = 'checkbox' name='box[]' value=\"".$rows['id']."\"'</td>"; echo"<td bgcolor='#FFFFFF'>".$rows['id']."</td>"; echo"<td bgcolor='#FFFFFF'>".$rows['login']."</td>"; echo"<td bgcolor='#FFFFFF'>".$rows['application']."</td>"; echo"<td bgcolor='#FFFFFF'>".$rows['password']."</td></tr>"; } ?> <tr><td colspan =6 align=center><input type=submit value=Delete name='delete'></form></td></tr></table> <?php if($_POST['box']){ $box = $_POST['box']; while (list ($key,$val) = @each ($box)) { $sqldel="DELETE FROM logins WHERE id='$val'"; $resdel=mysql_query($sqldel); echo"Record $val has been deleted."; } } else{ echo"No rows selected."; } ?> <a href="main_page.php">Return to main page</a> </center> </div> </body> </html> |
|
|
Oct 13 2009, 02:34 PM
Post
#2
|
|
|
Newbie ![]() Group: Members Posts: 11 Joined: 28-September 09 Member No.: 111,868 |
Well, I found my answer on my own, so hopefully it will help someone else that might have the same problem.
Sorry about not using the code tags before. I'm sure that's why I never got an answer, but that's ok. I figured it out. Here's the working code: CODE <?php
if(isset($_POST['delete'])){//check to see if the delete button has been pressed if(isset($_POST['box'])){ //check to see if any boxes have been checked $num = 0;//used to count the number of rows that were deleted $box = $_POST['box']; while (list ($key,$val) = @each ($box)) { //loop through all the checkboxes $num++; $sqldel="DELETE FROM logins WHERE id='$val'";//delete any that match id $resdel=mysql_query($sqldel);//send the query to mysql } //print the logs that were deleted echo"$num record(s) have been deleted."; } else{//no boxes checked echo "No records selected."; } } ?> |
|
|
Oct 13 2009, 03:26 PM
Post
#3
|
|
|
Outrageously Uber Ninja ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Moderators Posts: 1,805 Joined: 10-July 08 From: UK Member No.: 44,994 |
Have you considered foreach rather than that while(list = each) loop?
Swordz |
|
|
Oct 13 2009, 08:43 PM
Post
#4
|
|
|
Newbie ![]() Group: Members Posts: 11 Joined: 28-September 09 Member No.: 111,868 |
Have you considered foreach rather than that while(list = each) loop? Swordz Yeah, I tried that, but for some reason, the foreach didn't like that box[] was an array. Then, somebody suggested the while loop, and it worked fine, so I went with it. What would be the benefits of using one over the other? |
|
|
Oct 13 2009, 11:43 PM
Post
#5
|
|
|
Outrageously Uber Ninja ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Moderators Posts: 1,805 Joined: 10-July 08 From: UK Member No.: 44,994 |
As far as I can tell they're identical - just foreach is easier to understand.
swordz |
|
|
![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users) |
||
| 0 Members: | ||
Forum Jump |
||
| Lo-Fi Version | Time is now: 25th May 2013 - 04:50 PM |