【问题标题】:Multi-insertion with foreach 2 level array loop使用 foreach 2 级数组循环进行多插入
【发布时间】:2012-01-24 04:27:57
【问题描述】:

我正在使用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

【问题讨论】:

  • 你有没有尝试过?
  • 我正在尝试使用 stmt 插入,但我想知道是否有更好的方法来做到这一点

标签: php mysql foreach insertion


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    如果您的 $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(....,....,....,...,...)');
    }
    ?>
    

    【讨论】:

    • 请停止推广古老的mysql_* 功能。它们正在被弃用,您不应编写任何包含它们的新代码。相反,您应该学习如何将 PDOMySQLi 与准备好的语句一起使用。
    猜你喜欢
    • 2015-12-25
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2013-04-05
    • 2019-07-25
    相关资源
    最近更新 更多