【发布时间】:2016-05-31 20:46:11
【问题描述】:
编辑: 问题在 foreach 循环中得到解决。由于 foreach 循环只说应该将新成分插入到成分数据库中,它并没有说它也应该将其插入到 recipe_ingredients 所以我必须在 foreach 循环中的 insertIngredient 函数之后添加insertRecipeIngredients($link, $recipe_id, $ingredient_id, $unit, $amount, $i)($)
我不知道标题是否正是我所要求的,但我们拭目以待!我对这一切都很陌生..所以请尽量让答案对新手友好(:
我有这个 foreach 循环(解决方案标记在里面 - - -):
$i = 0;
foreach($ingredients as $ingredient) {
$ingredient_id = getIngredientId ($link, $ingredient);
if($ingredient_id != false) {
insertRecipeIngredient($link, $recipe_id, $ingredient_id, $unit, $amount, $i);
} else {
$ingredient_id = insertIngredient($link, $ingredient);
--- insertRecipeIngredient($link, $recipe_id, $ingredient_id, $unit, $amount, $i); ---
}
$i++;
}
并将这段代码插入数据库:
function insertRecipeIngredient($link, $recipe_id, $ingredient_id, $unit, $amount, $i) {
mysqli_query($link, "INSERT INTO recipe_ingredients (recipe_id, ingredient_id,
unit, amount) VALUES ('$recipe_id','$ingredient_id', '$unit[$i]', '$amount[$i]')");
}
这可行,但它只在数组中插入第一个成分。在我添加 $unit[$i] 和 $amount[$i] 之前,它工作得很好。 (这是错误的,它只插入了已经在成分库中的成分,而不是新的成分)
所有变量都来自一个表单:
$unit = $_POST['unit'];
$amount = $_POST['amount'];
etc...
我想如何将其余的数组放入数据库?
这是我的表格:
<form method="post" action="form_process.php">
<input type="text" name="title" placeholder="Titel">
<input type="text" name="portions" placeholder="Antal portioner">
<div class="ingredient_wrap">
<div class="first_ingredient">
</div>
</div>
<textarea name="instructions" placeholder="Beskrivning"></textarea>
<input type="submit" name="submit" value="Skicka">
</form>
这是数量/成分/单位的脚本:
function createUnitSelect() {
var units = ['g', 'ml', 'dl'];
var select = "";
$(units).each(function() {
select = select + '<option value="'+this+'">'+this+'</option>';
});
return '<select name="unit[]">' + select + '</select>';
}
// All of the variables
var inputAmount = '<input type="text" name="amount[]" placeholder="Mått">';
var inputUnit = createUnitSelect();
var inputIngredient = '<input type="text" name="ingredient[]" placeholder="Ingrediens">';
var firstIngredient = $(".first_ingredient");
var wrapper = $(".ingredient_wrap");
var buttonAdd = '<button class="add_ingredient">+</button>';
var buttonRemove = '<button class="remove_ingredient">-</button>';
var maxIngredients = 20;
var x = 1;
// The first ingredient-row
$(inputAmount).appendTo(firstIngredient);
$(inputUnit).appendTo(firstIngredient);
$(inputIngredient).appendTo(firstIngredient);
$(buttonAdd).appendTo(firstIngredient);
// Button for adding ingredient and doing so
$(wrapper).on("click", ".add_ingredient", function(e) {
e.preventDefault();
if(x < maxIngredients) {
x++;
$(wrapper).append('<div>' + inputAmount + inputUnit + inputIngredient + buttonAdd + '</div>') ;
}
$(this).removeClass("add_ingredient");
$(this).addClass("remove_ingredient");
$(this).text('-');
return false;
});
// Button for removing ingredient and doing so
$(wrapper).on("click", ".remove_ingredient", function(e) {
e.preventDefault();
$(this).parent('div').remove();
x--;
return false;
});
当我执行print_r($_POST) 时,我得到:
Array ( [title] => 肉丸 [partions] => 4 [amount] => Array ( [0] => 100 [1] => 2 [2] => 100 ) [unit] => Array ( [0] => g [1] => dl [2] => ml ) [成分] => 数组 ([0] => 肉 [1] => 工会 [2] => 西红柿) [说明] =>混合吃[提交] => Skicka )
这就是我获取配方 ID 和成分 ID 的方式:
食谱
// Insert the recipe
mysqli_query($link, "INSERT INTO recipes (title, portions, instructions) VALUES ('$title', '$portions', '$instructions')") or die(mysqli_error());
$recipe_id = mysqli_insert_id($link);
成分
// Get id of ingredient in database if it exist
function getIngredientId ($link, $ingredient) {
$result = mysqli_query($link, "SELECT * FROM ingredients WHERE ingredient='$ingredient'");
if($result) {
$row = mysqli_fetch_assoc($result);
return $row['id'];
} else {
return false;
}
}
【问题讨论】:
-
你使用 和 ??在 HTML 中?
-
如果你这样做
print_r($_POST)你会得到什么样的结果输出? -
没有 [ ] 就没有单位和数量
-
(旁注:此代码易受 SQL 注入攻击,请尝试使用准备好的语句)
-
好像有很多空字段~可以把你的表格加到问题里吗?
标签: php mysql arrays foreach relational-database