Solved

PHP foreach DB query

Posted on 2016-10-11
4
33 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
Comment Utility
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
Comment Utility
Hi,

I still only get 1 result from the query ?

Thanks Simon
0
 

Author Comment

by:pc-buddy
Comment Utility
I stand corrcted, its working

Thanks
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Button Click 11 24
PHP code to retrieve NDFD weather forecasts. 6 35
WEB Farm 6 23
uploading multiple image with php 14 16
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

15 Experts available now in Live!

Get 1:1 Help Now