Solved

PHP foreach DB query

Posted on 2016-10-11
4
51 Views
Last Modified: 2016-10-11
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
0
Comment
Question by:pc-buddy
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 500 total points
ID: 41838015
FIrst of all I'd check for the result of the very first query.
Furthermore how do you check if there's any error?
Let's say that without assigning the first query to a result, in your loop the code could go to execute the second query while the first is already working.
So I'd do something like:
foreach($_POST['selectedchild'] as $id)
{
		$insert_result = mysql_query("INSERT INTO outings (child_id) VALUES(".$id.")") or die(mysql_error());;
		  
		$query = "select email,email2 FROM child WHERE id = ".$id."";
		$result = mysql_query($query ) or die(mysql_error());;

		$email=mysql_result($result ,$i,'email');
		$email2=mysql_result($result ,$i,'email2');

		$mail->Addbcc($email);
		if ($email2 != ""){
		$mail->Addbcc($email2);
		
		}
}

Open in new window

0
 

Author Comment

by:pc-buddy
ID: 41838031
Hi,

I still only get 1 result from the query ?

Thanks Simon
0
 

Author Comment

by:pc-buddy
ID: 41838099
I stand corrcted, its working

Thanks
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41838167
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

2

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This video teaches users how to migrate an existing Wordpress website to a new domain.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now