0

I follow the solution from this example: PDO Prepared Inserts multiple rows in single query , i dont have any error but insert doesnt work. This is the code:

$pdo = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'username', 'password');
        $fields = array('field1', 'field2',  'field3', 'field4');

        $pdo->beginTransaction();
        $insert_values = array();
        foreach ($dataForInsert as $key => $value) {
            $question_marks[] = '(' . $this->placeholders('?', sizeof($value)) . ')';
            $insert_values = array_merge($insert_values, array_values($value));
        }

        $sql = "INSERT INTO table (" . implode(",", $fields ) . ") VALUES " . implode(',', $question_marks);
        $stmt = $pdo->prepare($sql);
        try {
            $stmt->execute($insert_values);
        } catch (PDOException $e){
            echo $e->getMessage();
        }

where i am making mistake?

Community
  • 1
  • 1
zafirov
  • 392
  • 1
  • 3
  • 18
  • did you debug your code there may be some hidden sql warnings... – Linus Jul 20 '15 at 07:58
  • print_r($stmt) gives: PDOStatement Object ( [queryString] => INSERT INTO live_collection_topic (field1,field2,field3,field4) VALUES (?,?,?,?),(?,?,?,?) – zafirov Jul 20 '15 at 08:00
  • print_r($insert_values) gives: [0] => 10591 [1] => 0062100000046 [2] => [3] => 4 [4] => 3835 [5] => 817 [6] => [7] => – zafirov Jul 20 '15 at 08:01
  • you are not defining the array in `foreach` my opinion it should be `foreach ($fields as $key => $value)` – Shehary Jul 20 '15 at 08:02
  • $collectionTopicData is my data that i got from stored procedure, data that i want to insert. it gives me proper data that i posted in previous coment – zafirov Jul 20 '15 at 08:04

0 Answers0