Php SQL插入多行,foreach

Php SQL插入多行,foreach,php,mysql,arrays,Php,Mysql,Arrays,根据返回数组的迭代次数,我正在尝试将多个行插入数据库中 insert正在工作,但无论数组中的内容是什么,都不会插入超过1行 function createOrder(){ $CustomerID = $_SESSION['CustomerID']; $BasketID = $_SESSION['BasketID']; // create a new entry with an OrderID $orders = new Basket; $orders->storeF

根据返回数组的迭代次数,我正在尝试将多个
插入数据库中

insert正在工作,但无论数组中的内容是什么,都不会插入超过1行

function createOrder(){

  $CustomerID = $_SESSION['CustomerID'];
  $BasketID = $_SESSION['BasketID'];

  // create a new entry with an OrderID
  $orders = new Basket;
  $orders->storeFormValues( $_POST );
  // Collect the OrderID returned from insertOrder(); and insert into 'Orders'
  $OrderID = $orders->insertOrder($CustomerID);

  // Populate OrderDetails with items in users Basket.
  $data = Basket::getBasket($BasketID);
  $results['basket'] = $data['results'];

  // Insert the order details into the orderDetails DB.
  $orders->insertOrderDetails($OrderID, $BasketID, $CustomerID, $results); 
};
循环:

public static function insertOrderDetails($OrderID, $BasketID, $CustomerID, $results){
   $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );

    // for each row insert into the DB
    foreach ( $results['basket'] as $row ) {
      $sql = "INSERT INTO OrderProducts (OrderID, ProductName, Price, Quantity) 
              VALUES (:OrderID, :ProductName, :Price, :Quantity)";

      $st = $conn->prepare( $sql );
      $st->bindValue( ":OrderID", $OrderID, PDO::PARAM_INT );
      $st->bindValue( ":ProductName", $row->ProductName, PDO::PARAM_STR );
      $st->bindValue( ":Price", $row->Price, PDO::PARAM_INT );
      $st->bindValue( ":Quantity", $row->Quantity, PDO::PARAM_STR );
      $st->execute();

   }
    $conn = null;
}
而数组,
$results
看起来像

array(1) {
  ["basket"]=>
  array(2) {
    [0]=>
    object(Basket)#3 (10) {
      ["OrderID"]=>
      NULL
      ["CustomerID"]=>
      NULL
      ["OrderItemID"]=>
      NULL
      ["ProductID"]=>
      string(1) "9"
      ["Quantity"]=>
      string(1) "4"
      ["ProductName"]=>
      string(12) "Cheese Bagel"
      ["Price"]=>
      string(1) "1"
      ["NameType"]=>
      string(5) "Bagel"
      ["BasketProductID"]=>
      string(2) "25"
      ["BasketID"]=>
      string(1) "3"
    }
    [1]=>
    object(Basket)#5 (10) {
      ["OrderID"]=>
      NULL
      ["CustomerID"]=>
      NULL
      ["OrderItemID"]=>
      NULL
      ["ProductID"]=>
      string(1) "2"
      ["Quantity"]=>
      string(1) "1"
      ["ProductName"]=>
      string(15) "The British BLT"
      ["Price"]=>
      string(1) "3"
      ["NameType"]=>
      string(5) "Bagel"
      ["BasketProductID"]=>
      string(2) "26"
      ["BasketID"]=>
      string(1) "3"
    }
  }
}
非常感谢您的任何建议

读取,您可能需要在执行下一条语句之前关闭游标

注意: 注: 某些驱动程序需要在执行下一条语句之前关闭游标


另一个注意事项是,您可能不必在每次迭代中创建语句。

可能需要尝试以下插入查询变量:

insert into tablename (id,blabla) values(1,'werwer'),(2,'wqewqe'),(3,'qwewe');
例如:

$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );

foreach ( $results['basket'] as $key => $row ) {
    $sql = "INSERT INTO OrderProducts (OrderID, ProductName, Price, Quantity) VALUES ";
    $sql .= "(:OrderID" . $key . ", :ProductName" . $key . ", :Price" . $key . ", :Quantity" . $key . "),";
}

$sql = substr($sql, 0, -1);
$st = $conn->prepare( $sql );

foreach ( $results['basket'] as $key => $row ) {
    $st->bindValue( ":OrderID" . $key, $OrderID, PDO::PARAM_INT );
    $st->bindValue( ":ProductName" . $key, $row->ProductName, PDO::PARAM_STR );
    $st->bindValue( ":Price" . $key, $row->Price, PDO::PARAM_INT );
    $st->bindValue( ":Quantity" . $key, $row->Quantity, PDO::PARAM_STR );
}
$st->execute();

两个foreach,但一个insert查询到数据库。

数据库中我的主键未设置为自动递增。改变这一点解决了问题。一旦允许,将删除。感谢您的帮助

您的主键和唯一键是什么?PDO默认使用“return boolean false”表示失败。您没有检查任何DB代码中的故障,这意味着您只是假设任何东西都不会出现故障。@Legionar,在
OrderProducts
中,OrderProductID是主要且唯一的,
Orders
,OrderID是主要的@MarcB谢谢,我计划在脚本的主要部分工作后创建一些Conidtion来检查。嗯,mysqli在OP的代码中的什么位置?它使用PDO抱歉,Marc,我错过了这个细节:)谢谢Dmitry,我不确定如何单独填充每一组括号?我没有测试它,但我认为应该使用这个变体(编辑我的答案)。谢谢Dmitry,一旦我将AI添加到DB中,你的解决方案就可以工作了。你介意解释一下我的剧本是否有好处吗?谢谢:)当然可以。我认为我的变体插入速度会更快,因为对数据库的单个查询比多个查询更快。但是如果记录的数量很小,那么您将看不到差异。关于这一点的一些问题:有意义,如果我有时间,我将更新以包括作为首选方法。非常感谢。是的,我给你写了评论,问你一个问题,你的主键和唯一键是什么?:-)