0

我正在使用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();
}

产品表:

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

产品属性表:

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

2 回答 2

2

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

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

这种方法的缺点是您无法使用准备好的语句,从而获得内置防止注入的额外好处。

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

于 2011-12-20T06:37:07.690 回答
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
        )

)

然后你可以这样 foreach :

<?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(....,....,....,...,...)');
}
?>
于 2011-12-20T06:43:36.070 回答