Page 1 of 1

PHP MySQL checkbox array

Posted: Sat Oct 10, 2009 10:32 am
by EleventhDecibel
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 (my host) 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: Select all

<!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>

Re: PHP MySQL checkbox array

Posted: Thu Oct 15, 2009 11:31 am
by akuji36
Hello

Follow this link to video tutorial

http://www.learningnerd.com/phpmysql-day-5



thanks
rod
http://www.webpagesofease.com