• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

PHP Question... checkboxes and deleting with a database...

Al Neri

Diamond Member
Okay I have this code for the admin panel for my guestbook....

Basically what it outputs is for each entry the following in a table...

[ ] Name Email WEbsite Comment

[ ] Name Email WEbsite Comment

[ ] Name Email WEbsite Comment

etc.

(( I bolded what I called the checkboxes in the code. ))

when i submit what is the best way to basically say

if checked = true then {sql$="DELETE from Guestbook WHERE name=$name, email=$email, website=$website', comment=$comment


What is the best way to do this, I can't seem to find this anywhere on the 'net...


The table's columns are as such

Guestbook
IDNO
Name
email
website
comments
_________________


here's the code:

<html><body>

<?php
//link db begin
$a=0;
$link = mysql_connect('xxx', 'xxx', 'xxx')
or die('Could not connect: ' . mysql_error());
mysql_select_db(xxx) or die('Could not select database');
//link db end


//actual code begin


$query2 = "SELECT * FROM Guestbook";
$result = mysql_query($query2) or die('Query failed: ' . mysql_error());
Echo "<table border cellpadding=3>";

?><form method="POST" action="adminb.php"> <?

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
Echo "\n\n\n<table border cellpadding=0>";
Echo "<tr><Td>";
$a++;
echo "<input type=\"checkbox\" name=\"admin[]\">";
echo "</td><td width=25>Name:</td><td width=250>".$line['name']."</td>";
Echo "<td>E-Mail:</td><td>".$line['email']."</td>";
Echo "<td>Website:</td><td>".$line['website']."</td>";
Echo "<td>Comment:</td><td>".$line['comment']."</td></tr></table>\n\n\n<br>";
}//actual code end
?>
<input type="submit" value="submit" name="submit">
</body></html>
 
How about this:

Add a value property to your checkboxes that is the IDNO of the record:
echo "<input type=\"checkbox\" name=\"admin[]\" value=\"$line['idno']\" />";

Then on your adminb.php page have something like the following code below:
 
Thanks!!


I'm trying to implement it...


before i forget to do this... if I or anyone else ever comes back, the syntax was a lil off with your checkbox...

echo "<input type=\"checkbox\" name=\"admin[]\" value=\"".$line['idnum']."\" />";

works fine.
 
<html>
<head>
<title>adminb.php</title
</head>
<body>
<?php
$a=0;
$admin_array = $_POST['admin'];

$link = mysql_connect('xxx', 'xxx', 'xxx')
or die('Could not connect: ' . mysql_error());
mysql_select_db('xxx') or die('Could not select database');


foreach ($admin_array as $line) {
$sql = "DELETE FROM Guestbook WHERE idnum = $line;";
$result = mysql_query($sql)or die('Could not connect: ' . mysql_error());
$a++;
}
echo "<BR/><font color=\"red\">".$a.' deletions complete. new listings displayed below:<br /><br/><br><br>';

$query2 = "SELECT * FROM Guestbook $limit";
$result = mysql_query($query2) or die('Query failed: ' . mysql_error());
Echo "<table border cellpadding=3>";

?><form method="POST" action="adminb.php"> <?

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
Echo "\n\n\n<table border cellpadding=0>";
Echo "<tr><Td>";
$a++;



echo "<input type=\"checkbox\" name=\"admin[]\" value=\"".$line['idnum']."\" />";



echo "</td><td width=25>Name:</td><td width=250>".$line['name']."</td>";
Echo "<td>E-Mail:</td><td>".$line['email']."</td>";
Echo "<td>Website:</td><td>".$line['website']."</td>";
Echo "<td>Comment:</td><td>".$line['comment']."</td></tr></table>\n\n\n<br/>";
}//actual code end
?>
<input type="submit" value="submit" name="submit">
</body></html>

</body>
</html>


😀😀😀😀
 
Back
Top