I'm getting an error:

Parse error: syntax error, unexpected '"', expecting ']' at the line - '$_POST[ingredientAMT" . $i . "]',

I have the following code:

for ($i=1; $i<=$_POST[ingre_amt]; $i++)
{
$SQL1 = "INSERT INTO 
recipeingredients (MealID, IngreAmt, UnitID, IngreID) 
VALUES (
'$_POST[mealID]', 
'$_POST[ingredientAMT" . $i . "]', 
'$_POST[ingredientUNIT" . $i . "]', 
'$_POST[ingredientNAME" . $i . "]')";
} 

I also tried.....

for ($i=1; $i<=$_POST[ingre_amt]; $i++)
{
$SQL1 = "INSERT INTO 
recipeingredients (MealID, IngreAmt, UnitID, IngreID) 
VALUES (
'$_POST[mealID]', 
'$_POST[ingredientAMT"; 
$SQL1 .= $i;
$SQL1 .= "]', '$_POST[ingredientUNIT";
$SQL1 .= $i; 
$SQL1 .= "]', '$_POST[ingredientNAME";
$SQL1 .= $i;
$SQL1 .= "]')";
} 

I don't know how many ingredient will be inputted, so I need to have $i go in there.

Here is the HTML code if you need to look at it

<form action="recipe_info_post.php" method="post">
<?php 
if (isset($NumIngred))
{	
for ($i=1; $i<=$NumIngred; $i++)
  {
  echo "<tr>";
  echo "<td> <input type='text' name='ingredientAMT" . $i . "' size=1 /></td>";
  echo "<td><select name='ingredientUNIT" . $i . "'><option>" . $unit_options . "</option></select> </td>";
  echo "<td><select name='ingredientNAME" . $i . "'><option>" . $ingre_options . "</option></select> </td>";
  echo "</tr>";

  } 		
}
?>	

    you should learn how to create forms to send variables by arrays.

    <select name="content[]"><option value="a">AAA</option></select>
    <select name="content[]"><option value="a">AAA</option></select>
    <select name="content[]"><option value="a">AAA</option></select>
    <select name="content[]"><option value="a">AAA</option></select>

    This will post $_POST["content"] as an array. You can get its selected values
    by foreach() or add $i as an index name.

      What database extension are you using? You need to escape the incoming variables, and one good way to do so is to prepare a statement and then reuse it in the loop.

        I am using MySQL (phpmyadmin). How would I "then reuse it in the loop"?

        I think djjjozsi's way might be what I need to do here.

        Where would I pull the $i variable?

        somethings like $_POST[content[$i]] ???

        OR...

        for ($ID=1; $AMT=1; $UNIT= 1; $INGRE=1; $ID<=$POST[ingre_amt];
        $ID++; $AMT++; $UNIT++; $INGRE++)
        {
        $SQL1 = "INSERT INTO
        recipeingredients (MealID, IngreAmt, UnitID, IngreID)
        VALUES (
        '$
        POST[$ID]',
        '$POST[$AMT]',
        '$
        POST[$UNIT]',
        '$_POST[$INGRE]')";
        }

          Change the repeated form fields as array names, and on submit,
          see the structure of you $POST array.
          You can get the lenght of this array.

          echo count($_POST["ingredientAMT"];

          With this lenght you can get the matched values by for() or foreach()...
          See the manual about these functions.

          <?php
          for( $i = 0;$i < count( $_POST["ingredientAMT"] ) ; $i++ )
          {
              echo "$i.  ingredientAMT is: " . $_POST["ingredientAMT"][$i];
          } 
          
          ?>
          

          And of course Laserlight is right.

            First, sorry I meant to say I think I'll go djjjozsi's way. Not that only djjjozsi is right.

            I just realized in my source page i don't have an array after all. Here it is

            for ($i=1; $i<=$NumIngred; $i++)
              {
              echo "<tr>";
              echo "<td> <input type='text' name='ingredientAMT" . $i . "' size=1 /></td>";
              echo "<td><select name='ingredientUNIT" . $i . "'><option>" . $unit_options . "</option></select> </td>";
              echo "<td><select name='ingredientNAME" . $i . "'><option>" . $ingre_options . "</option></select> </td>";
              echo "</tr>";
            
              } 
            

            I guess the easiest way is to do it as an array uh? Is there anyway to keep it is now and change the code I have in my first post (would that be laserlight's way)?

              i show you on the first line:

                echo "<td> <input type='text' name='ingredientAMT[]' size=1 /></td>"; 

              Because [] will generate automatic indexing, started from 0.

                Write a Reply...