Jump to content

Problems with foreach() statement and HTML Checkboxes


DannyM

Recommended Posts

Hello,

 

I'm having some trouble with my PHP script. I have a foreach() statement that goes through and assigns values according to what the user checked. However, if the user does not check anything, errors are thrown. How would I silence these errors, since the user does not have to check anything?

 

Heres a bit of my code:

 


if(isset($_POST['approve'])) {

foreach($_POST['user'] as $a) {
$result=@mysql_query("UPDATE members SET approved='0' WHERE username='$a'");
}

foreach($_POST['topic'] as $b) {
$result=@mysql_query("UPDATE forum_question SET APPROVED='0' WHERE TOPIC='$b'");
}

foreach($_POST['reply'] as $c) {
$result=@mysql_query("UPDATE forum_answer SET a_APPROVED='0' WHERE a_TOPIC='$c'");
}

}


echo"
<html>
<head>

<style type='text/CSS'>
font{
color: #FFFFFF;
font-weight: 900;
}

a{
color: #44FF44;
}

.quickReply{
background-color: #a77a99;
border: 2px solid;
border-color: #FFFFFF;
}

.blackFont{
color: #000000;
}

.tdBackground{
background-color: #000099;
}
</style>


<title>
Forums
</title>

<body bgcolor='#000000' vlink='#ff8888'>

<center><h1><font>Check to approve or delete</font></h1></center>
<font><h2>New Users Waiting to be verified:</h2></font>
<hr/>
<br/>
<form method='POST' action='approve.php'/>";

$result=mysql_query("SELECT * FROM members WHERE approved='1'");
while($row=mysql_fetch_array($result))
{
echo "<input type='checkbox' name='user[]' value='".$row['username']."'> <font> $row[username] </font><br/>";
}

echo "<br/><br/><h1><font>New Topics waiting to be approved</font></h1><hr/>";
$result=mysql_query("SELECT * FROM forum_question WHERE APPROVED='1'");
while($row=mysql_fetch_array($result))
{
$name=$row['name'];
$email=$row['email'];
$body=$row['detail'];
$title=$row['topic'];

echo '<input type="checkbox" name="topic[]" value="'.$title.'"> <font> <table border="2"><tr><td><font>' . $title . ' by ' . $name . '(' . $email . ')<br/>
<br/>
<p>' . $body . '</font>
</td></tr>
</table>
</font><br/>';

}


echo "<br/><br/><h1><font>New Replies waiting to be approved</font></h1><hr/>";
$result=mysql_query("SELECT * FROM forum_answer WHERE a_APPROVED='1'");
while($row=mysql_fetch_array($result))
{
$title=$row['topic'];
$name=$row['a_name'];
$email=$row['a_email'];
$body=$row['a_detail'];

echo '<input type="checkbox" name="reply[]" value="'.$title.'"> <font> <table border="2"><tr><td><font> Reply from ' . $name . '(' . $email . ')<br/>
<br/>
<p>' . $body . '</font>
</td></tr>
</table>
</font><br/>';

}

//Echo the Approve and Delete Buttons
echo "
<input type='submit' value='Approve' name='approve'/> <input type='submit' value='Delete' name='delete'/>
</form>";




 

Maybe you would add a condition to check if the user checked a box within a group of boxes. For example:

 

if (isset($_POST['user']))
{
    foreach($_POST['user'] as $a) 
   {
        $result=@mysql_query("UPDATE members SET approved='0' WHERE username='$a'");
    }
}

 

 

 

Maybe you would add a condition to check if the user checked a box within a group of boxes. For example:

 

if (isset($_POST['user']))
{
    foreach($_POST['user'] as $a) 
   {
        $result=@mysql_query("UPDATE members SET approved='0' WHERE username='$a'");
    }
}

 

 

 

This, too, works. I didn't know you could check for checkboxes and other things!

Thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.