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\htdocsecoverecover.php:47 Stack trace: #0 C:\xampp\htdocsecoverecover.php(47): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocsecoverecover.php on line 47
This is my code:
// 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());
}
}
I am new to PHP, pretty much, and many people telling me to start using PDO, before mysql_ will be out dated.
So I started, and it seems really hard. Trying to get used to it.
What did I do wrong? Thanks!
The date
and time
are keywords. Please escape them using backticks.
You are missing the parentheses for your VALUES
clause.
The prepare
statement should be:
$insert = $connect->prepare(
"INSERT INTO users ( username, password, message, `date`, `time`, id )
VALUES( :username, :password, :message, CURDATE(), CURTIME(), :id )"
);
VALUES doesn't have a closing bracket.