简体   繁体   English

第一个PDO语法有效,第二个类似的PDO语法无效

[英]First PDO syntax works, second similar one doesn't

I'm trying to create two databases after people fill out a signup form with their username, password, and email. 人们尝试使用用户名,密码和电子邮件填写注册表单后,我试图创建两个数据库。

The first MySQL query below works. 下面的第一个MySQL查询有效。 It inserts their info in the user table. 它将其信息插入到用户表中。

The second query doesn't though. 第二个查询没有。 It's trying to insert an encrypted key in the confirm table that I can use to confirm their account with a confirmation email. 它正在尝试在确认表中插入加密密钥,我可以使用该密钥通过确认电子邮件来确认其帐户。

Here's the PDO exception message: 这是PDO异常消息:

exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 消息“ SQLSTATE [42000]”的异常“ PDOException”:语法错误或访问冲突:1064 SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key, email) VALUES ( '24', 'd96e77df9072c73da2612380a784f51b', 'r' at line 1' in 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 {main} 请检查与您的MySQL服务器版本相对应的手册,以获取在“密钥,电子邮件附近”使用的正确语法。 -up-tutors \\ index.php:80堆栈跟踪:#0 C:\\ xampp \\ htdocs \\ tutorspot \\ sign-up-tutors \\ index.php(80):PDOStatement-> execute()#1 {main}

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();
    }
}

key and user are reserved words in SQL syntax. keyuserSQL语法中的保留字。 Wrap your columns and table names in backticks. 将列和表名包装在反引号中。 For example: 例如:

(`userid`, `key`, `email`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM