Help - Search - Members - Calendar
Full Version: Mysql Checkbox Delete
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
cogs
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>
cogs
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.";
   }
}
?>
swordz
Have you considered foreach rather than that while(list = each) loop?

Swordz
cogs
QUOTE(swordz @ Oct 13 2009, 03:26 PM) *
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?
swordz
As far as I can tell they're identical - just foreach is easier to understand.

swordz
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.