PHP PDO多插入不起作用

时间:2015-07-20 07:54:11

标签: php mysql pdo insert

我遵循此示例中的解决方案:PDO Prepared Inserts multiple rows in single query ,我没有任何错误,但插入不起作用。这是代码:

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

我犯了错误?

0 个答案:

没有答案