Link to home
Start Free TrialLog in
Avatar of pc-buddy
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.

<input name='selectedchild[]' type='checkbox' value='{$row['id']}'>
           <input name='selectedchild[]' type='checkbox' value='{$row['id']}'>
           <input name='selectedchild[]' type='checkbox' value='{$row['id']}'>

Open in new window


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);
		
		}
}

Open in new window


Thanks Simon
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pc-buddy
pc-buddy

ASKER

Hi,

I still only get 1 result from the query ?

Thanks Simon
I stand corrcted, its working

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

$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
}

Open in new window