Paul Dessert
Paul Dessert

Reputation: 6389

PDO statement not inserting

Is there any reason this PDO insert statement isn't inserting in 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();

I'm not getting any errors. Everything looks correct to me.

Upvotes: 0

Views: 72

Answers (3)

Petah
Petah

Reputation: 46050

Your query should be:

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

Or:

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

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

You should be able to check for errors like this:

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

Or:

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

Or:

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

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

Upvotes: 5

databyss
databyss

Reputation: 6488

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

Check your INSERT syntax http://dev.mysql.com/doc/refman/5.1/en/insert.html

Upvotes: 3

Peter
Peter

Reputation: 1798

Yes, you're missing SET:

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

Upvotes: 4

Related Questions