PDO SQL错误(插入时)

时间:2013-03-22 01:39:25

标签: php pdo

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1' in C:\xampp\htdocs\recover\recover.php:47 Stack trace: #0 C:\xampp\htdocs\recover\recover.php(47): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\recover\recover.php on line 47

这是我的代码:

// This checks if variabes are not empty, and if username is less than 11 characters long.
if (!empty($username) && !empty($password) && !empty($message) && strlen($username) < 11) {

    // Shortcut for our ID generator function.
    $ID = generateID();

    // Now lets insert (register the account) these datas to our database.
    $insert = $connect->prepare("INSERT INTO users (username, password, message, date, time, id) VALUES (:username, :password, :message, CURDATE(), CURTIME(), :id");

    // Executing in array, because if we binded all these variables, it would look very bad.
    $insert->execute(array(
        ':username' => $username,       
        ':password' => $password, 
        ':message' => $message, 
        ':id' => $ID
    ));

    if (!$insert->execute()) {
        print_r($insert->errorInfo());
    }       
}

我是PHP新手,很多人告诉我开始使用PDO,之后mysql_会过时。

所以我开始了,看起来真的很难。 试着习惯它。

我做错了什么? 谢谢!

2 个答案:

答案 0 :(得分:3)

VALUES没有结束括号。

答案 1 :(得分:2)

datetime是关键字。请用反引号逃脱它们。


您缺少VALUES子句的括号。


prepare语句应为:

$insert = $connect->prepare(
    "INSERT INTO users ( username, password, message, `date`, `time`, id )
        VALUES( :username, :password, :message, CURDATE(), CURTIME(), :id )"
);