0

を使用して複数の挿入を行っていますforeach(各製品には多くの属性がある可能性があるため、2つのレベルのループがあります)。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
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 に答える