简体   繁体   English

如何遍历动态表单输入数组并将它们插入多个表中?

[英]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). 我想我需要一个数组来保存成分输入(ingredient_name,数量,度量类型),然后以某种方式遍历该数组,直到它被填满为止(用户可以单击“添加新成分”按钮,然后jQuery添加一组新的输入)。 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). ...我真正遇到的问题是需要将“ ingredient_name”输入插入一个表(成分),而将“ amount”和“ measurement type”输入插入另一表(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();

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. 当发布到您的PHP脚本时,它们将是您可以循环遍历的数组。

$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();
}
}

Took out the queries and pdo stuff. 拿出查询和pdo的东西。

                $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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM