2011-12-20 4 views
0

foreach을 사용하여 다중 삽입을 수행하고 있습니다 (각 제품마다 많은 속성이있을 수 있기 때문에 두 단계의 루핑이 있습니다). stmt를 사용하도록 제안했으나이를 수행하는 방법을 모르십시오.foreach 2 레벨 배열 루프로 다중 삽입

데이터를 데이터베이스에서 가져 오는 방법을 알고 있습니다. 데이터를 데이터베이스에 저장하는 데 도움이 필요합니다.

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] =>))) 

코드 :

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

표 제품 속성 :

는 MySQL의에서
id---product_id---code---color---size---stock 
+0

당신은 아무것도 시도? – Indranil

+0

메신저 stmt 함께 삽입하려고하지만이 일을 더 나은 방법이 궁금하다면 –

답변

2

한 번에 여러 행을 삽입 할 수 있습니다 prodcut에 대한

// 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(); 
} 

테이블

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

이 방법의 단점은 준비된 문을 사용할 수 없어 주입에 대한 기본 보호 기능이 추가된다는 것입니다.

또는 준비된 문을 만든 다음 루프에서 매개 변수를 사용하여 실행할 수 있습니다. 이것은 더 느린 방법이지만 데이터를 삽입하기 전에 데이터를 수동으로 위생 처리 할 필요가 없습니다. 당신의 $product 배열은 다음과 같이하면

1

:

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(....,....,....,...,...)'); 
} 
?> 
+2

고대 'mysql_ *'함수를 홍보하는 것을 중지하십시오. 그들은 더 이상 사용되지 않는 과정에 있으며 당신이 그들을 포함하는 새로운 코드를 작성해서는 안됩니다. 그 대신 [PDO] (http://php.net/pdo) 또는 [MySQLi] (http://php.net/mysqli)를 준비된 문과 함께 사용하는 방법을 배워야합니다. –

+0

그리고 당신이 잘못 했어 ... –