PDO multiple inserts


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $pdo->beginTransaction();
  2. //...
  3. $stmt = $pdo->prepare('INSERT INTO foo VALUES(:v1_1, :v1_2, :v1_3),
  4. (:v2_1, :v2_2, :v2_3),
  5. (:v2_1, :v2_2, :v2_3)');
  6. $stmt->bindValue(':v1_1', $data[0][0]);
  7. $stmt->bindValue(':v1_2', $data[0][1]);
  8. $stmt->bindValue(':v1_3', $data[0][2]);
  9. //...
  10. $stmt->execute();
  11.  
  12. //or
  13.  
  14. $stmt = $pdo->prepare('INSERT INTO foo VALUES(:a, :b, :c)');
  15. foreach($data as $item)
  16. {
  17. $stmt->bindValue(':a', $item[0]);
  18. $stmt->bindValue(':b', $item[1]);
  19. $stmt->bindValue(':c', $item[2]);
  20. $stmt->execute();
  21. }
  22. //...
  23. $pdo->commit();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.