使用foreach 2级数组循环进行多次插入

时间:2011-12-20 06:22:59

标签: php mysql foreach insertion

使用foreach进行多次插入(有两级循环,因为每个产品可能有很多属性)。建议使用stmt,但不知道如何做这些。

我知道从表单中检索数据的方式。我需要有关将数据放入数据库的帮助。

Array ( [1] => Array ( 
[category] => 1 
[code] => NFK50889922
[price] => 15.00 [name] => Pendants 
[description] => Gold pendants covered with 400k diamond 
[thumbnail] => 131120091585.jpg 

//second level array for attribute
[attcode] => Array ( [0] => [1] => [2] => ) 
[color] => Array ( [0] => [1] => [2] => ) 
[size] => Array ( [0] => [1] => [2] => ) 
[stock] => Array ( [0] => [1] => [2] => ) ) )

代码:

    // Check for a form submiss
    if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

    $product=$_POST['product'];


    foreach($product as $productcount){

    $q = 'INSERT INTO product(id,code,name,description,category_id,price,icon) VALUES (NULL,'.$productcount['code'].',"'.$productcount['name'].'",'.$productcount['description'].',"'.$productcount['category'].',"'.$productcount['price'].',"'.$productcount['thumbnail'].')';

    mysqli_query($dbc, $q);//insertion of general information of current product 


    //insertion of many attribute of current product
    $sql = 'INSERT INTO product_attribute (product_id,code,c_value,s_value,stock) VALUES (LAST_INSERT_ID(), ?, ?, ?, ?)';

            // Prepare the statement:
            $stmt = mysqli_prepare($dbc, $sql);



    // For debugging purposes:
        // if (!$stmt) echo mysqli_stmt_error($stmt);

        mysqli_stmt_bind_param($stmt,'sssi',$attribute_code,$color_value,$size_value,$stock_unit);

         foreach($productcount['code'] as $attcode){
            $attribute_code=$attcode;
            }

         foreach($productcount['color'] as $attcolor){
            $color_value=$attcolor;
            }

         foreach($productcount['size'] as $attsize){
            $size_value=$attsize;
            }

         foreach($productcount['stock'] as $attstock){
            $stock_unit=$attstock;
            }

         foreach($productcount['attcode'] as $attcode){ 
            $attcode;
            }

        // Execute the query:
        mysqli_stmt_execute($stmt);
        $stmt->close();
}

prodcut表:

id---code---name---description---categori_id---price

产品属性表:

id---product_id---code---color---size---stock

2 个答案:

答案 0 :(得分:2)

在mysql中,您可以一次插入多行:

INSERT INTO TableName( 
   foo_field, 
   bar_field 
) 
VALUES 
   ( foo1, bar1 ), 
   ( foo2, bar2 ),
   ( foo3, bar3 ),
   ( foo4, bar4 ) 

此方法的缺点是您无法使用预先准备好的陈述,从而获得内置的注射保护的额外好处。

或者,您可以创建一个预准备语句,然后在循环中使用参数执行它。这将是一种较慢的方式,但您不需要在插入数据之前手动清理数据。

答案 1 :(得分:1)

如果您的$product数组看起来像这样:

Array
(
    [0] => Array
        (
            [name] => thename1
            [color] => thecolor1
            [size] => thesize1
            [stock] => thestock1
            [attcode] => theattcode1
        )

    [1] => Array
        (
            [name] => thename2
            [color] => thecolor2
            [size] => thesize2
            [stock] => thestock2
            [attcode] => theattcode2
        )

)

然后你可以像这样预告:

<?php

foreach($product as $k=>$v)
{
    $name = $product[$k]['name'];
    $color = $product[$k]['color'];
    $size =  $product[$k]['size'];
    $stock = $product[$k]['stock'];
    $attcode = $product[$k]['attcode'];

    $mysqli->query('INSERT INTO table(product_id,code,color,size,stock) VALUES(....,....,....,...,...)');
}
?>