Solved

PHP foreach DB query

Posted on 2016-10-11
4
77 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 56

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

733 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