crarls
crarls

Reputation: 31

How to loop through an array of dynamic form inputs and insert them into more than one table?

I have a master form for inserting recipes into a database. Most of the form is working except for being able to insert more than one ingredient into the database.

I think I need an array to hold the ingredient inputs (ingredient_name, amount, measurement type) and then to somehow loop through that array as many times as it has been filled out (user can click an 'add new ingredient' button and jquery adds a new set of inputs). I could probably figure this out on my own as this has been asked before, but...

...the real problem I'm having is that the 'ingredient_name' input needs to be inserted into one table (ingredients) and the 'amount' and 'measurement type' inputs need to be inserted into another table (ingredientLists). I have no idea how to do this.

<input type="text" name="ingredient_name" value="" />
<input type="text" name="amount" value="" />
<select name="measurement_ID" value=""> 
   <option value="14" >n/a</option> 
   <option value="1"  >teaspoon</option> 
   <option value="2"  >tablespoon</option> 
   <option value="3"  >fluid ounce</option> 
   <option value="4"  >cup</option> 
   <option value="5"  >pint</option> 
   <option value="6"  >quart</option> 
   <option value="7"  >pound</option> 
   <option value="8"  >ounce</option> 
   <option value="9"  >milligram</option> 
   <option value="10" >gram</option> 
   <option value="11" >millimeter</option> 
   <option value="12" >centimeter</option> 
   <option value="13" >inch</option>
</select>

I'm not sure if I can even process it correctly using the form set-up I have now.

This is what I have processing the ingredient inputs so far (this set-up only allows the first ingredient to be entered into the database).

                $recipe_id = $pdo->lastInsertId('recipe_ID');

                //inputs inserted into ingredient table
                $query2 = $pdo->prepare('INSERT INTO ingredients (ingredient_name) VALUES (?)');
                $query2->bindValue(1, $ingname);

                $query2->execute();

                $ingredient_id = $pdo->lastInsertId('ingredient_ID');

                //inputs inserted into ingredient list table
                $query3 = $pdo->prepare('INSERT INTO ingredientLists (recipe_ID, ingredient_ID, amount, measurement_ID) VALUES (?,?,?,?)');
                $query3->bindValue(1, $recipe_id);
                $query3->bindValue(2, $ingredient_id);
                $query3->bindValue(3, $amount);
                $query3->bindValue(4, $measure);

                $query3->execute();

Upvotes: 1

Views: 1255

Answers (2)

crarls
crarls

Reputation: 31

Took out the queries and pdo stuff.

                $ingname     = $_POST['ingredient_name'];
                $amount      = $_POST['amount']; //ingredient amount
                $measure     = $_POST['measurement_ID']; //ingredient measurement


                for($i=0;$i<=count($ingname);$i++) {
                    if (isset($ingname[$i])){
                        print_r($_POST['ingredient_name']);
                    }
                }

When I print the array containing the ingredient names, this is the result. It just repeats the array four times, since I entered four ingredients. I'm not sure if this is good or bad.

Array ( [0] => cookies [1] => candy [2] => chocolate [3] => cake ) Array ( [0] => cookies [1] => candy [2] => chocolate [3] => cake ) Array ( [0] => cookies [1] => candy [2] => chocolate [3] => cake ) Array ( [0] => cookies [1] => candy [2] => chocolate [3] => cake )

Upvotes: 0

Funk Doc
Funk Doc

Reputation: 1643

In order to submit multiple lines you need to put them in an array (note the brackets after the name)

<input type="text" name="ingredient_name[]" value="" />
<input type="text" name="amount[]" value="" />
<select name="measurement_ID[]"> 
<option value="14" >n/a</option> 
<option value="1"  >teaspoon</option> 
<option value="2"  >tablespoon</option> 
<option value="3"  >fluid ounce</option> 
<option value="4"  >cup</option> 
<option value="5"  >pint</option> 
<option value="6"  >quart</option> 
<option value="7"  >pound</option> 
<option value="8"  >ounce</option> 
<option value="9"  >milligram</option> 
<option value="10" >gram</option> 
<option value="11" >millimeter</option> 
<option value="12" >centimeter</option> 
<option value="13" >inch</option>
</select>

When posted to your php script they will be arrays that you can loop through.

$ingredient_name=$_POST['ingredient_name'];
$amount=$_POST['amount'];
$measurement_ID=$_POST['measurement_ID'];
for($i=0;$i<=count($ingredient_name);$i++) {
if (isset($ingredient_name[$i])){
//do your insert queries here you can insert to any tables you need to.
//the variables are accessed like this $ingredient_name[$i] 
}
}

Below is using your code

$recipe_id = $pdo->lastInsertId('recipe_ID');

$ingname=$_POST['ingredient_name'];
$amount=$_POST['amount'];
$measure=$_POST['measurement_ID'];

for($i=0;$i<=count($ingname);$i++) {
if (isset($ingname[$i])){
$query2 = $pdo->prepare('INSERT INTO ingredients (ingredient_name) VALUES (?)');
$query2->bindValue(1, $ingname[$i]);
$query2->execute();
$ingredient_id = $pdo->lastInsertId('ingredient_ID');

$query3 = $pdo->prepare('INSERT INTO ingredientLists (recipe_ID, ingredient_ID, amount, measurement_ID) VALUES (?,?,?,?)');
$query3->bindValue(1, $recipe_id);
$query3->bindValue(2, $ingredient_id);
$query3->bindValue(3, $amount[$i]);
$query3->bindValue(4, $measure[$i]);
$query3->execute();
}
}

Upvotes: 1

Related Questions