如何通过动态表单输入数组循环,并把它们插入到多个表? [英] How to loop through an array of dynamic form inputs and insert them into more than one table?
问题描述
我对食谱插入到数据库中的主表。大部分的形式下工作,除了能够插入多个成分到数据库中。
我想我需要一个数组来保存成分输入(ingredient_name,数量,计量型),然后以某种方式通过该数组多次循环,因为它已经被填写了(用户可以单击添加新的成分按钮和jQuery增加了一套新的输入)。我大概可以算出这个对我自己,因为这已经被问过,但...
<强> ...我有真正的问题是,'ingredient_name'输入需要被插入到一个表(组分)和量和测量类型输入需要被插入到另一个表(ingredientLists)。我不知道如何做到这一点。
&LT;输入类型=文本名称=ingredient_nameVALUE =/&GT;
&LT;输入类型=文本名称=数量值=/&GT;
&LT;选择名称=measurement_IDVALUE =&GT;
&LT;期权价值=14将N / A&LT; /选项&GT;
&LT;期权价值=1&GT;&茶匙LT; /选项&GT;
&LT;期权价值=2&GT;&汤匙LT; /选项&GT;
&LT;期权价值=3&GT;液体盎司&LT; /选项&GT;
&LT;期权价值=4&GT; CUP&LT; /选项&GT;
&LT;期权价值=5&GT;&品脱LT; /选项&GT;
&LT;期权价值=6&GT;&夸脱LT; /选项&GT;
&LT;期权价值=7&GT;英镑&LT; /选项&GT;
&LT;期权价值=8&GT;&每盎司LT; /选项&GT;
&LT;期权价值=9&GT;&毫克LT; /选项&GT;
&LT;期权价值=10&GT;&克LT; /选项&GT;
&LT;期权价值=11&GT;&毫米LT; /选项&GT;
&LT;期权价值=12&GT;&厘米LT; /选项&GT;
&LT;期权价值=13&GT;英寸和LT; /选项&GT;
&LT; /选择&GT;
我不知道如果我能它正确使用形式建立我现在甚至已经处理。
这是我处理的原料输入到目前为止(这种设置只允许输入到数据库中的第一个成分)。
$ recipe_id = $ pdo-&GT; lastInsertId('recipe_ID'); //输入插入成分表
$ QUERY2 = $ pdo-&GT; prepare(INSERT INTO成分(ingredient_name)VALUES()?');
$ query2-&GT; bindValue(1,$ ingname); $ query2-&GT;执行(); $ ingredient_id = $ pdo-&GT; lastInsertId('ingredient_ID'); //输入插入成分表的表
$ QUERY3 = $ pdo-&GT; prepare(INSERT INTO ingredientLists(recipe_ID,ingredient_ID,金额,measurement_ID)VALUES(,,,)????');
$ query3-&GT; bindValue(1,$ recipe_id);
$ query3-&GT; bindValue(2,$ ingredient_id);
$ query3-&GT; bindValue(3,$金额);
$ query3-&GT; bindValue(4,$措施); $ query3-&GT;执行();
为了提交你需要把它们放入数组(注意名称后括号)
多行 &LT;输入类型=文本名称=ingredient_name []值=/&GT;
&LT;输入类型=文本名称=量[]值=/&GT;
&LT;选择名称=measurement_ID []&GT;
&LT;期权价值=14将N / A&LT; /选项&GT;
&LT;期权价值=1&GT;&茶匙LT; /选项&GT;
&LT;期权价值=2&GT;&汤匙LT; /选项&GT;
&LT;期权价值=3&GT;液体盎司&LT; /选项&GT;
&LT;期权价值=4&GT; CUP&LT; /选项&GT;
&LT;期权价值=5&GT;&品脱LT; /选项&GT;
&LT;期权价值=6&GT;&夸脱LT; /选项&GT;
&LT;期权价值=7&GT;英镑&LT; /选项&GT;
&LT;期权价值=8&GT;&每盎司LT; /选项&GT;
&LT;期权价值=9&GT;&毫克LT; /选项&GT;
&LT;期权价值=10&GT;&克LT; /选项&GT;
&LT;期权价值=11&GT;&毫米LT; /选项&GT;
&LT;期权价值=12&GT;&厘米LT; /选项&GT;
&LT;期权价值=13&GT;英寸和LT; /选项&GT;
&LT; /选择&GT;
当张贴到你的PHP脚本,他们将是数组,你可以通过循环。
$ ingredient_name = $ _ POST ['ingredient_name'];
$金额= $ _ POST ['量'];
$ measurement_ID = $ _ POST ['measurement_ID'];
为($ I = 0; $ I&LT; =计数($ ingredient_name); $ I ++){
如果(使用isset($ ingredient_name [$ i])){
//做你的插入查询,在这里你可以插入到你需要的任何表。
//变量都是这样$ ingredient_name [$ i]访问
}
}
下面是使用code
$ recipe_id = $ pdo-&GT; lastInsertId('recipe_ID');$ ingname = $ _ POST ['ingredient_name'];
$金额= $ _ POST ['量'];
$措施= $ _ POST ['measurement_ID'];为($ I = 0; $ I&LT; =计数($ ingname); $ I ++){
如果(使用isset($ ingname [$ i])){
$ QUERY2 = $ pdo-&GT; prepare(INSERT INTO成分(ingredient_name)VALUES()?');
$ query2-&GT; bindValue(1,$ ingname [$ i]);
$ query2-&GT;执行();
$ ingredient_id = $ pdo-&GT; lastInsertId('ingredient_ID');$ QUERY3 = $ pdo-&GT; prepare(INSERT INTO ingredientLists(recipe_ID,ingredient_ID,金额,measurement_ID)VALUES(,,,)????');
$ query3-&GT; bindValue(1,$ recipe_id);
$ query3-&GT; bindValue(2,$ ingredient_id);
$ query3-&GT; bindValue(3,$金额[$ i]);
$ query3-&GT; bindValue(4,$措施[$ i]);
$ query3-&GT;执行();
}
}
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();
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();
}
}
这篇关于如何通过动态表单输入数组循环,并把它们插入到多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!