0

这个 PDO 插入语句没有插入 MySQL 有什么原因吗?

$stmt = $dbh->prepare("INSERT INTO training courseId = :postCourse, title = :postCourseName, startDate = :postStartDate, endDate = :postEndDate");
$stmt->bindParam(':postCourse', $postCourse);
$stmt->bindParam(':postCourseName', $postCourseName);
$stmt->bindParam(':postStartDate', $postStartDate);
$stmt->bindParam(':postEndDate', $postEndDate);
$stmt->execute();

我没有收到任何错误。一切对我来说都是正确的。

4

3 回答 3

5

您的查询应该是:

INSERT INTO training (courseId, title, startDate, endDate) VALUES
(:postCourse, :postCourseName, :postStartDate, :postEndDate);

或者:

INSERT INTO training 
SET courseId = :postCourse, 
    title = :postCourseName,
    startDate = :postStartDate, 
    endDate = :postEndDate

http://dev.mysql.com/doc/refman/5.5/en/insert.html

您应该能够检查以下错误:

$stmt = $dbh->prepare(...);
if (!$stmt) {
    var_dump($dbh->errorInfo());
}

或者:

$stmt->execute();
var_dump($stmt->errorInfo());

或者:

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
...

见:http ://www.php.net/manual/en/pdo.error-handling.php

于 2012-08-23T22:26:16.523 回答
4

是的,你错过了SET

$stmt = $dbh->prepare("INSERT INTO training SET courseId = :postCourse, title = :postCourseName, startDate = :postStartDate, endDate = :postEndDate");
于 2012-08-23T22:25:47.017 回答
3
$stmt = $dbh->prepare("INSERT INTO training (courseId, title, startDate, endDate) VALUES (:postCourse, :postCourseName, :postStartDate, :postEndDate");

检查您的 INSERT 语法http://dev.mysql.com/doc/refman/5.1/en/insert.html

于 2012-08-23T22:26:49.767 回答