2015-03-03 175 views
0

enter image description herePdo多行插入問題

我在使用pdo準備語句在mysql中插入多行時出現了一些問題。

該插入工作,但只有一次。

我試圖把foreach之前的查詢,但同樣的問題。

$product_id = $_POST['id']; 
    if (isset($_POST['product_attribute'])){ 
     $att_id = array(); 

    foreach ($_POST['product_attribute'] as $product_attribute) { 

     $att_id[] = $product_attribute['attribute_id']; 

     if ($product_attribute['attribute_id']) { 
      $stmt = $database->prepare("DELETE FROM product_attribute WHERE product_id =:product_id AND attribute_id=:attribute_id"); 
      $stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT); 
      $stmt->bindParam(':attribute_id', $product_attribute['attribute_id'], PDO::PARAM_INT); 
      $stmt->execute(); 


     foreach ($product_attribute['product_attribute_description'] as $product_attribute_description) { 
      $stmt = $database->prepare("INSERT INTO product_attribute SET product_id=:product_id, product_code=:product_code, attribute_id=:attribute_id, `text`=:atext"); 
      $stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT); 
      $stmt->bindParam(':product_code', $_POST['codprodus'], PDO::PARAM_INT); 
      $stmt->bindParam(':attribute_id', $product_attribute['attribute_id'], PDO::PARAM_INT); 
      $stmt->bindParam(':atext', $product_attribute_description['text'], PDO::PARAM_STR); 
      $stmt->execute(); 

     } 
     } 
    } 

    $vals = implode(",", $att_id); 
    $stmt = $database->prepare("DELETE FROM product_attribute WHERE product_id =:product_id AND attribute_id NOT IN(:vals)"); 
    $stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT); 
    $stmt->bindParam(':vals', $vals, PDO::PARAM_STR); 
    $stmt->execute(); 

    }else { 
    $stmt = $database->prepare("DELETE FROM product_attribute WHERE product_id =:product_id"); 
    $stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT); 
    $stmt->execute(); 
    } 

的print_r($ _ POST [ 'product_attribute']:

Array([103] => Array(
      [attribute_id] => 103 
      [product_attribute_description] => Array 
       (
        [2] => Array 
         (
          [text] => 56 
         ) 

       ) 

     ) 

    [102] => Array 
     (
      [attribute_id] => 102 
      [product_attribute_description] => Array 
       (
        [2] => Array 
         (
          [text] => da 
         ) 

       ) 

     ) 

    [104] => Array 
     (
      [attribute_id] => 104 
      [product_attribute_description] => Array 
       (
        [2] => Array 
         (
          [text] => da 
         ) 

       ) 

     ) 

) 

這裏是我的product_attribute表模式

product_id  int(11)  NO PRI   
attribute_id int(11)  NO PRI   
product_code int(11)  NO   
text   text  NO 
+0

試試看從'$ product_attribute_description ['text']''移動'['text']''因爲您正在做'product_attribute_description' – 2015-03-03 14:19:10

+0

如果我刪除它,結果將會是數組 – speedy 2015-03-03 14:21:17

+0

您是否嘗試過'implode()'? – 2015-03-03 14:21:57

回答

1

此代碼是刪除新插入的行評吧。並且一切都很好。

/* 
$vals = implode(",", $att_id); 
$stmt = $database->prepare("DELETE FROM product_attribute WHERE product_id =:product_id AND attribute_id NOT IN(:vals)"); 
$stmt->bindValue(':product_id', $product_id, PDO::PARAM_INT); 
$stmt->bindValue(':vals', $vals, PDO::PARAM_STR); 
$stmt->execute(); 
*/ 
+0

@speedy,非常感謝 - 很高興能夠協助它的運行。 – 2015-03-04 20:01:30