我試圖在人們用他們的用戶名,密碼和電子郵件填寫註冊表單後創建兩個數據庫。第一個PDO語法起作用,第二個類似的不起作用
下面的第一個MySQL查詢的作品。它將他們的信息插入到用戶表中。
雖然第二個查詢沒有。它試圖在確認表中插入一個加密密鑰,我可以用它來確認他們的賬戶並附上確認郵件。
這裏的PDO異常消息:
異常 'PDOException' 有消息「SQLSTATE [42000]:語法錯誤 或訪問衝突:1064您的SQL語法錯誤;檢查 對應於您的MySQL服務器版本的手冊,右邊的 語法用於在'key',email附近使用]值:('24', 'd96e77df9072c73da2612380a784f51b','1'處的'r' C:\ xampp \ htdocs \ tutorspot \ sign-up-tutors \ index.php:80 Stack trace:#0 C:\ xampp \ htdocs \ tutorspot \ sign-up-tutors \ index.php(80): PDOStatement-> execute()# {1}主要
if($action['result'] != 'error'){
$password = md5($password . 'salty');
try
{
$sql = 'INSERT INTO user (fullname, username, email, password, active) VALUES (
:fullname,
:username,
:email,
:password,
0)';
$s = $pdo->prepare($sql);
$s->bindValue(':fullname', $_POST['fullname']);
$s->bindValue(':username', $_POST['username']);
$s->bindValue(':email', $_POST['email']);
$s->bindValue(':password', $password);
$s->execute();
//get the new user id
$userid = $pdo->lastInsertId();
}
catch (PDOException $e)
{
$error = 'Error inserting signup info.';
include $_SERVER['DOCUMENT_ROOT'] . '/tutorspot/inc/error.html.php';
exit();
}
//create a random key for confirmation
$key = $username . $email . date('mY');
$key = md5($key . 'salty');
//add info to confirm table
try
{
$sql = 'INSERT INTO confirm (userid, key, email) VALUES (
:userid,
:key,
:email)';
$s = $pdo->prepare($sql);
$s->bindValue(':userid', $userid);
$s->bindValue(':key', $key);
$s->bindValue(':email', $_POST['email']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error inserting confirm info.';
include $_SERVER['DOCUMENT_ROOT'] . '/tutorspot/inc/error.html.php';
echo $e;
exit();
}
}
'USER',它是你的表的名字,是一個MySQL保留字。不要使用表名或列名的保留字,如果你這樣做(你永遠不應該這樣做),用反引號引用它們。 – 2013-02-08 13:27:57