pc-buddy
asked on
PHP foreach DB query
Hi,
I have a long list of tick boxes where i select multipul items, i then have a foreach once the form is submited where i'm trying to do a datebase query, but it dopesnt work I can see the id'd being passed over but the query will only run once, what is the best practice to do this.
Thanks Simon
I have a long list of tick boxes where i select multipul items, i then have a foreach once the form is submited where i'm trying to do a datebase query, but it dopesnt work I can see the id'd being passed over but the query will only run once, what is the best practice to do this.
<input name='selectedchild[]' type='checkbox' value='{$row['id']}'>
<input name='selectedchild[]' type='checkbox' value='{$row['id']}'>
<input name='selectedchild[]' type='checkbox' value='{$row['id']}'>
foreach($_POST['selectedchild'] as $id)
{
mysql_query("INSERT INTO outings (child_id) VALUES(".$id.")");
$query = "select email,email2 FROM child WHERE id = ".$id."";
$result = mysql_query($query );
$email=mysql_result($result ,$i,'email');
$email2=mysql_result($result ,$i,'email2');
$mail->Addbcc($email);
if ($email2 != ""){
$mail->Addbcc($email2);
}
}
Thanks Simon
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I stand corrcted, its working
Thanks
Thanks
You might want to do some sanity checking on the $_POST before using it in a foreach. You have no guarantee that was is posted is what you expect.
Something like
Something like
$selected = isset($_POST['selectedchild']) ? $_POST['selectedchild'] : false;
if ($selected && is_array($selected)) {
// selectedchild is set AND it is an array so proceed with foreach
foreach($selected as $id) {
...
}
}
else {
// handle invalid input
}
ASKER
I still only get 1 result from the query ?
Thanks Simon