php - Pdo多行插入问题

标签 php mysql oop pdo

enter image description here

我在使用 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['产品属性'] :

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

最佳答案

此代码正在删除新插入的行。注释掉它,一切都很好。

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

关于php - Pdo多行插入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28833929/

相关文章:

php - 同一 MYSQL 查询中的父消息和子消息

mysql - SQLSTATE[HY000] : errno: 150 "Foreign key constraint is incorrectly formed

c# - 如何制作一个公共(public)子类来删除重复代码

java - 由于未知原因而出现运行时错误;引用对象输入流

php - Eclipse - 堆栈溢出错误

php - 使用 PHP 和 AJAX 更新 MySQL 数据库

php-mysql 从数据库中获取下一个和上一个 id

php - 有什么方法可以在 PHP 中安全地重新声明一个类?

php - 架构/部署决策

php - 如果为 NULL,则验证 mysql 查询