First of all, I'm kinda new to this and don't know if the title is exactly what I'm asking for so I'll apologize in forehand. (I'm also Swedish so my English might not be perfect). And I looked at the "similar questions" but I didn't find anything of interest so far.
I have this foreach loop:
$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++;
}
and I use this to insert the connection between recipe and ingredients in my database (and I want to insert the amount and unit of that ingredient):
function insertRecipeIngredient($link, $recipe_id, $ingredient_id) {
mysqli_query($link, "INSERT INTO recipe_ingredients (recipe_id, ingredient_id, unit,
amount) VALUES ('$recipe_id','$ingredient_id', '$unit[$i]', '$amount[$i]'")); }
it says that the variables aren't defined (the unit and amount). I tried to echo out the whole thing inside of the foreach loop and it worked. So I guess that the extension with [$i] is right, but I don't know how to use it for the INPUT.
echo $amount[$i].' '.$unit[$i].' - '.$ingredient;
So the connection between the recipe(id) and the ingredients(id) are working just fine, but the amount and units doesn't wanna get into the database.
How am I suppose to insert those arrays into the database?
Do you need any more information?