0

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?

Pixeldoris
  • 99
  • 7
  • Where is `$unit` coming from? You're not passing it in as an argument in your function. – DiddleDot Feb 18 '16 at 22:54
  • all of the unit, amount, ingrediens, recipe information etc. if from a form. so $unit = $_POST['unit']. is that what you mean? – Pixeldoris Feb 18 '16 at 22:56
  • 1
    In your `insertRecipeIngredient()` function you are not passing in `$unit` as an argument so that function does not know what `$unit` is (unless you have it set as a global). It's also not passing in `$i` for that matter. – DiddleDot Feb 18 '16 at 22:57
  • ^^ and inserting in a loop is a bad idea. concatenate and insert just once –  Feb 18 '16 at 22:58
  • Dagon, why is it bad to insert in a loop? how would I else insert an array? – Pixeldoris Feb 18 '16 at 23:07
  • You're also trying to use `$i` inside the function, but the scope of `$i` won't make it available inside the function. If you really want to use it inside `insertRecipeIngredient()` then you need to put `global $i` inside the function before you actually use it. You'll need to add `$unit` and `$amount` as globals as well if you're not going to send them to the function as an argument. – Drew Feb 18 '16 at 23:08
  • the less inserts the better (resource usage): as to how:http://stackoverflow.com/questions/12502032/insert-multiple-rows-with-one-query-mysql –  Feb 18 '16 at 23:09
  • Diddle Dot, so it should be function insertRecipeIngredient(..., $unit, $amount)? how will i know it's set as a global? I though anything inside the and outside anything else was a global? $unit $amount and $i are just in the – Pixeldoris Feb 18 '16 at 23:09
  • functions cannot access variables set outside themselves unless they're passed to the function as an argument, or defined as a global inside the function - so you'd have to go, `function insertRecipeIngredient($link, $recipe_id, $ingredient_id, $unit, $amount) { global $i; ... }` - just as a note, I'm not advocating this as the best method, but it will solve your problem without doing any major revisions. Alternative, you could just send $i to the function each time `function insertRecipeIngredient($link, $recipe_id, $ingredient_id, $unit, $amount, $i) {` – Drew Feb 18 '16 at 23:11
  • Drew, I'll try that. – Pixeldoris Feb 18 '16 at 23:12
  • `insertRecipeIngredient(..., $unit, $amount, $i)` – DiddleDot Feb 18 '16 at 23:12
  • I did the `insertRecipeIngredient(..., $unit, $amount, $i)` .. but now I get : Warning: mysqli_error() expects exactly 1 parameter, 0 given in /m1/home/jonheh132/public_html/testing/form_process.php on line 45 . and that's the line for `mysqli_query($link, "INSERT INTO recipe_ingredients (recipe_id, ingredient_id, unit, amount) VALUES ('$recipe_id', '$ingredient_id', '$unit[$i]', '$amount[$i]'))` – Pixeldoris Feb 18 '16 at 23:20

1 Answers1

0

Change your if statement to this:

if($ingredient_id != false) {
    insertRecipeIngredient($link, $recipe_id, $ingredient_id, $unit, $amount, $i);
}

And change that function to accept the $unit, $amount, and $i parameters

function insertRecipeIngredient($link, $recipe_id, $ingredient_id, $unit, $amount, $i)
DiddleDot
  • 745
  • 5
  • 16