这个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();
我没有犯任何错误。在我看来一切都是正确的。
您的查询应该是:
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
是的,您缺少SET
:
$stmt = $dbh->prepare("INSERT INTO training SET courseId = :postCourse, title = :postCourseName, startDate = :postStartDate, endDate = :postEndDate");
$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