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

标签 php mysql arrays foreach relational-database

编辑: 问题已在 foreach 循环中解决。由于 foreach 循环仅表示应将新成分插入成分数据库中,因此并没有说还应将其插入到 Recipe_ingredients 中,因此我必须添加 insertRecipeIngredients($link, $recipe_id, $ingredient_id, $ unit, $amount, $i)($) 就在 foreach 循环中的 insertIngredient 函数之后

<小时/>

我不知道标题是否正是我想要的,但我们拭目以待!我对这一切都很陌生..所以请尝试让答案对新手友好(:

我有这个 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]')");
}

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

Array ( [标题] => 肉丸 [份数] => 4 [数量] => Array ( [0] => 100 [1] => 2 [2] => 100 ) [unit] => Array ( [0] => g [1] => dl [2] => ml ) [成分] => 数组 ( [0] => 肉 [1] => 联合 [2] => 西红柿 ) [说明] =>混合并食用 [提交] => Skicka )

这就是我获取配方 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 注入(inject)攻击。更好的方法是使用准备好的语句在 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    |
+----+------------+

关于php - 将数组插入关系数据库仅适用于数据库中已存在的数组中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502581/

相关文章:

mysql - 在 X 行后拆分表并在 While 循环中添加文本(使用 TCPDF)

php - 事务内的Mysql事务

javascript - 一个关于javascript中Array继承的问题

javascript - 如何在离线时将 txt/csv 文件加载到 javascript 字符串/数组中

php - 根据键的值将数组拆分成更小的数组?

javascript - 显示内容 Jquery

javascript - 如何获取 data-id 的值并将其放入 twitter Bootstrap 模式中?

mysql - 如何简化node js查询中的嵌套if循环

php - 如何在 OpenCart 2.0.3.1 中从 OpenCart 1.5.6.4 导入产品

php - 如何在 Ubuntu 文本用户界面 (TUI) 上安装 Lampp?