Advertisement
cdsatrian

PDO insert multiple value

Oct 21st, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.76 KB | None | 0 0
  1. <?php
  2. //---
  3. function placeholders($text, $count=0, $separator=","){
  4.     $result = array();
  5.     if($count > 0){
  6.         for($x=0; $x<$count; $x++){
  7.             $result[] = $text;
  8.         }
  9.     }
  10.  
  11.     return implode($separator, $result);
  12. }
  13. $pdo->beginTransaction() // also helps speed up your inserts
  14. $insert_values = array();
  15. foreach($data as $d){
  16.  $question_marks[] = '('  . placeholders('?', sizeof($d)) . ')';
  17.  $insert_values = array_merge($insert_values, array_values($d));
  18. }
  19. $sql = "INSERT INTO table (" . implode(",", array_keys($datafield) ) . ") "
  20.       ."VALUES " . implode(',', $question_marks);
  21. $stmt = $pdo->prepare ($sql);
  22. try {
  23.     $stmt->execute($insert_values);
  24. } catch (PDOException $e){
  25.     echo $e->getMessage();
  26. }
  27. $pdo->commit();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement