将数组插入关系数据库仅适用于数据库中已存在的数组中的数据


Insert array into relational-database only work with data in the array that already exist in the database

EDIT: 该问题已在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]')");
}

这有效,但它只在数组中插入第一个 ingredinens。在我添加$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)时,我得到:

数组 ( [标题] => 肉丸 [份] => 4 [数量] => 数组 ( [0] => 100 [1] => 2 [2] => 100 ) [单位] => 数组 ( [0] => 克 [1] => 分升 [2] => 毫升 ) [成分] => 阵列 ( [0] => 肉 [1] => 联合 [2] => 西红柿 ) [说明] =>

混合和食用 [提交] => 斯基卡 )

这是我获取配方 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;
    }
}

根据发布的数据来支持您的问题(应该注意的是,直接在SQL中嵌入变量是一个坏主意,它可能导致SQL注入攻击。更好的方法是在mysqlipdo内使用准备好的语句

/* 
    For testing, to emulate the submitted 
    form with data you provided. You, of course,
    have the actual $_POST data from the form to
    work with.
*/
$_POST=array ( 
    'title'         => 'meatballs',
    'portions'      => 4,
    'amount'        => array ( 100, 2, 100 ), 
    'unit'          => array('g','dl','ml'),
    'ingredient'    => array ('meat','unions','tomatoes'),
    'instructions'  => 'Mix and eat',
    'submit'        => 'Skicka'
);
define( 'BR','<br />' );

/* 
 Rather than a separate function to get the ingredient_id 
 you could use a subquery in the insert statement
*/
function insertRecipeIngredient( $link, $recipe_id, $ingredient, $amount, $unit ) {
    /* ensure that the ingredient is in the table. */
    $sql="insert ignore into `ingredients` set `ingredient`='{$ingredient}';";
    mysqli_query( $link, $sql );
    echo $sql.BR;
    $sql="insert into `recipe_ingredients` ( `recipe_id`, `ingredient_id`, `amount`, `unit` )
            values
            (   
                '{$recipe_id}',
                ( select `id` from `ingredients` where `ingredient`='{$ingredient}' ),
                '{$amount}',
                '{$unit}'
            );";
    mysqli_query( $link, $sql );
    echo $sql.BR;
}
/* get variables from POSTed data */
$title=$_POST['title'];
$portions=$_POST['portions'];
$amounts=$_POST['amount'];
$units=$_POST['unit'];
$ingredients=$_POST['ingredient'];
$instructions=$_POST['instructions'];

$sql="INSERT INTO `recipes` ( `title`, `portions`, `instructions` ) VALUES ( '$title', '$portions', '$instructions' );";
mysqli_query( $link, $sql ) or die( mysqli_error() );
$recipe_id = mysqli_insert_id( $link );
/* for debugging - comment out later */
echo $sql;
/*
    Assuming that each of the arrays 
    have the same number of elements
*/
foreach( $amounts as $i => $qty ){
    $ingredient=$ingredients[ $i ];
    $amount=$amounts[ $i ];
    $unit=$units[ $i ];
    call_user_func( 'insertRecipeIngredient', $link, $recipe_id, $ingredient, $amount, $unit );
}
/* which outputs the following */
INSERT INTO `recipes` ( `title`, `portions`, `instructions` ) VALUES ( 'meatballs', '4', 'Mix and eat' );
insert into `recipe_ingredients` ( `recipe_id`, `ingredient_id`, `amount`, `unit` ) values ( '3', ( select `id` from `ingredients` where `ingredient`='meat' ), '100', 'g' );
insert into `recipe_ingredients` ( `recipe_id`, `ingredient_id`, `amount`, `unit` ) values ( '3', ( select `id` from `ingredients` where `ingredient`='unions' ), '2', 'dl' );
insert into `recipe_ingredients` ( `recipe_id`, `ingredient_id`, `amount`, `unit` ) values ( '3', ( select `id` from `ingredients` where `ingredient`='tomatoes' ), '100', 'ml' );

数据被正确插入(假设成分首先存在于ingredients表中)

mysql> select * from recipes;
+----+-----------+----------+--------------+
| id | title     | portions | instructions |
+----+-----------+----------+--------------+
|  1 | meatballs |        4 | Mix and eat  |
+----+-----------+----------+--------------+
mysql> select * from recipe_ingredients;
+----+-----------+---------------+--------+------+
| id | recipe_id | ingredient_id | amount | unit |
+----+-----------+---------------+--------+------+
|  1 |         1 |             1 |    100 | g    |
|  2 |         1 |             2 |      2 | dl   |
|  3 |         1 |             3 |    100 | ml   |
+----+-----------+---------------+--------+------+
mysql> select * from ingredients;
+----+------------+
| id | ingredient |
+----+------------+
|  1 | Meat       |
|  2 | Onions     |
|  3 | Tomatoes   |
|  4 | Carrots    |
+----+------------+