We help IT Professionals succeed at work.

PHP / MySQL using foreach to pass multiple POST variables into a MySQL table

marcoullis
marcoullis used Ask the Experts™
on
When i try to run this script I get the syntax error:

Parse error: syntax error, unexpected ',', expecting ')' in issues.class.php on line 873

Here is the code that corresponds with line 873, which is

foreach($body as $key =>$val, $subject as $key => $val2, $url as $key => $val3)

What i am trying to do is pass three $_POST variables $subject, $body and $url into a foreach loop for multiple insert actions on a MySQL table. What am I doing wrong here?


      $body = $_POST['body'];
      $subject = $_POST['subject'];
      $url = $_POST['url'];
      echo '<pre>';print_r($body);


      foreach($body as $key =>$val, $subject as $key => $val2, $url as $key => $val3)
{
	   
	
         $sql = "INSERT INTO ISSUES
              (
                PARENT_REF,
                SUBJECT,
                BODY,
                URL
              )
              VALUES
              (
                '".$parentID."',
                '".$val2."',
                '".$val."',
                '".$val3."'
              )";
                    mysql_query($sql);
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
wait -- read it too fast :p

foreach only works with one set.

foreach($url as $key => $value)

if you want it to run multiple times, either do it sequentially, or nested: eg

foreach($myitem as $key => $val )
 {
  foreach($val as $num1 => $num2)

... etc

Author

Commented:
could you show it in the code type please the solution you propose for

foreach($myitem as $key => $val )
 {
  foreach($val as $num1 => $num2)
Also, it seems as if you are over-complicating it.

If I can see your HTML form I can tell you exactly how to code it into php.

If you have multiple body's subject's and url's the html should look similar to:

<input type='text' name="url[0]"><input type='text' name="body[0]"><input type='text' name="subject[0]">
<input type='text' name="url[1]"><input type='text' name="body[1]"><input type='text' name="subject[1]">
<input type='text' name="url[2]"><input type='text' name="body[2]"><input type='text' name="subject[2]">

Open in new window


then in php you can do something ilke:

$ct = count($_REQUEST[$url]);
for($x=0;$x<$ct;$x++) {
$sql = "INSERT INTO `table` VALUES (" . $_REQUEST['url'][$x] ."," . $_REQUEST['subject'][$x] ."," . $_REQUEST['body'][$x] .")";

}

Open in new window


i think you get it from here

Author

Commented:
I don't think what i need is to run it multiple times. I need to pass all three variables into the INSERT INTO query, so that on each query, the posted variables get inserted into the database.

Author

Commented:
here is the form elements you requested:

 do { 
         
         echo "<tr>";   
         echo "<td><div><input type=checkbox name=\"body[]\" value=\"".$row_result['PARENT_ID']."\"></div></td>";
         
         // echo "<h2>Link Title</h2> \n";
         echo "<input name=\"subject[]\" type=\"hidden\" value=\"".$row_result['SUBJECT']."\" /></li> \n";
         echo "<input name=\"url[]\" type=\"hidden\" value=\"".$row_result['STATE']."\" /></li> \n";
         echo "<td> <b>".$row_result['SUBJECT']." </b>(".$row_result['PARENT_ID'].")</option> \n";
         echo "<br>(".$row_result['STATE']." - District ".$row_result['DISTRICT'].")";   
	 echo "<br><br></td>";
	 echo "</tr>";
	 

            
             } while ($row_result = mysql_fetch_assoc($result)); 

Open in new window

Author

Commented:
What you are suggesting looks like a major re-write of the functional php. Can we keep the current way of doing things, but focus on using nested foreach loops?

I was liking where you were going with that.
Fixer of Problems
Most Valuable Expert 2014
Commented:
Nested 'foreach' loops wont' do what you want.  Each 'foreach' loop goes thru it's entire array and does not synchronize with the outer loops which means that the three variables are not kept together in a group.  The code that I have seen that does what you're doing has always used numerical indexes in the form page to make sure the POST variables can be grouped properly.  This is especially true if there are 'radio' or 'checkbox' inputs because if they are left blank, they are not submitted and that throws the sync between variables off.