Insert array connected to foreach loop into database

Pixeldoris

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?

DiddleDot

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)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Textbox Array connected MySQL database

分類Dev

perl foreach array result from a database

分類Dev

How to loop array of objects using forEach?

分類Dev

Display 2 Array values in foreach() loop - PHP

分類Dev

(PHP) Accessing specific item in array with foreach loop

分類Dev

How to fetch data from array and insert to database

分類Dev

Adding array object to an array in a foreach loop if a condition is satisfied in php

分類Dev

Foreach loop stores only first index of multidimensional array | Laravel

分類Dev

How to create variable array name in foreach loop for perl

分類Dev

Refer to a value just set in array within foreach loop

分類Dev

concatinating two array elements with a string within a foreach loop

分類Dev

Is there a performance penalty for accessing count($array) each time in a foreach loop?

分類Dev

How to get column in 2-D array in java by Foreach loop?

分類Dev

How can I create the array index in a foreach loop?

分類Dev

forEach on an Array

分類Dev

Bash foreach loop

分類Dev

Group dates in a foreach loop

分類Dev

double forEach to double for Loop

分類Dev

Php foreach outside loop

分類Dev

Await forEach loop

分類Dev

Nested foreach loop in powershell

分類Dev

Laravel foreach loop

分類Dev

Foreach loop in jquery

分類Dev

How to drop a Redshfit database with connected users

分類Dev

Insert POINT into postgres database

分類Dev

PHP MySQL Insert database

分類Dev

Insert image into MySQL database

分類Dev

Mysql Insert date in a database

分類Dev

PHP: Insert array into an array