Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将连接到foreach循环的数组插入数据库_Php_Arrays_Mysqli_Foreach_Insert - Fatal编程技术网

Php 将连接到foreach循环的数组插入数据库

Php 将连接到foreach循环的数组插入数据库,php,arrays,mysqli,foreach,insert,Php,Arrays,Mysqli,Foreach,Insert,首先,我对这个有点陌生,不知道标题是否正是我想要的,所以我会用正手道歉。(我也是瑞典人,所以我的英语可能并不完美)。我看了“类似的问题”,但到目前为止我没有发现任何感兴趣的问题 我有一个foreach循环: $i = 0; foreach($ingredients as $ingredient) { $ingredient_id = getIngredientId ($link, $ingredient); if($ingredient_id != false) { insertRe

首先,我对这个有点陌生,不知道标题是否正是我想要的,所以我会用正手道歉。(我也是瑞典人,所以我的英语可能并不完美)。我看了“类似的问题”,但到目前为止我没有发现任何感兴趣的问题

我有一个foreach循环:

$i = 0;
foreach($ingredients as $ingredient) {

$ingredient_id = getIngredientId ($link, $ingredient);

if($ingredient_id != false) {
    insertRecipeIngredient($link, $recipe_id, $ingredient_id, $unit, $amount);
} else {
    $ingredient_id = insertIngredient($link, $ingredient);
}
$i++;
}
我用它在数据库中插入配方和成分之间的连接(我想插入成分的数量和单位):

它说变量没有定义(单位和数量)。我试着在foreach循环中重复整个过程,结果成功了。因此,我猜带有[$I]的扩展是正确的,但我不知道如何将其用于输入

echo $amount[$i].' '.$unit[$i].' - '.$ingredient;
因此,配方(id)和配料(id)之间的连接工作正常,但数量和单位不想进入数据库

我该如何将这些数组插入数据库


您需要更多信息吗?

将您的if语句更改为:

if($ingredient_id != false) {
    insertRecipeIngredient($link, $recipe_id, $ingredient_id, $unit, $amount, $i);
}
并将该函数更改为接受
$unit
$amount
$i
参数

function insertRecipeIngredient($link, $recipe_id, $ingredient_id, $unit, $amount, $i)

$unit
来自哪里?您没有将其作为参数传递到函数中。所有单位、金额、配料、配方信息等,如果来自表单。所以$unit=$\u POST['unit']。这就是您的意思吗?在
insertRecipeIngCredit()
函数中,您没有将
$unit
作为参数传递,因此该函数不知道
$unit
是什么(除非将其设置为全局参数)。在这方面,它也没有传入
$i
。^^^而在循环中插入是个坏主意。连接并只插入一次边,为什么在循环中插入不好?否则我将如何插入数组?它是否会给您带来错误?如果是的话,是不是和你之前提到的一样?是的,是相同的错误。这是我的全部代码:拿出
global$i
并告诉我它是做什么的。哦,我忘了在截图之前拿出那个。它仍然是相同的错误消息。将查询的
或die
部分更改为
或die(mysqli_error($link))
,它会给您一个更具体的错误。
function insertRecipeIngredient($link, $recipe_id, $ingredient_id, $unit, $amount, $i)