php - PDO 插入错误 - 执行(array());

标签 php mysql

我正在尝试建立一个注册系统,但收到此错误:

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 ''=L&myPRwgOSB?V_execute(Array) #1 {main} thrown in /home/**/public_html/forum/register-cmd.php on line 58

我不知道为什么。这是代码:有什么问题吗?

<?php
include('./includes/connect.php');
/* START VARIABLES */
$submit = $_POST['submit'];

$username = clean($_POST['username']);

$email = clean($_POST['email']);
$repeatedemail = clean($_POST['remail']);

$password = clean($_POST['password']);
$repeatedpassword = clean($_POST['rpassword']);

$year = (int) $_POST['year'];
$day = (int) $_POST['day'];
$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

if(in_array(ucwords($_POST['month']), $months)) {
    $dob = $_POST['month'] . " " . $day . ", " . $year;
} else {
    header('Location: register.php?error=nonexist');
}

$referrer = $_POST['referrer'];

$recieve_emails = (int) $_POST['getemail'];
$view_signatures = (int) $_POST['sig'];
/* END VARIABLES */
if(isset($username) && isset($submit) && isset($email) && isset($repeatedemail) && isset($password) && isset($repeatedpassword) && isset($year) && isset($day) && isset($_POST['month']) && isset($recieve_emails) && isset($view_signatures)) {
    if($email == $repeatedemail) {
        if($password == $repeatedpassword) {
            $check_user = $con->prepare("SELECT * FROM users WHERE username = :username");
            $check_user->execute(array(
                ":username" => $username
            ));
            if($check_user->rowCount() == 1) {
                header('Location: register.php?error=usernametaken');
            } else {
                $check_email = $con->prepare("SELECT * FROM users WHERE email = :email");
                $check_email->execute(array(
                    ":email" => $email
                ));
                if($check_email->rowCount() == 1) {
                    header('Location: register.php?error=emailtaken');
                } else {
                    $salt = salt();

                    $hashedPassword = sha1($pepper . $password . $salt);
                    $insert = $con->prepare("INSERT INTO users ('username', 'password', 'salt', 'email', 'dob', 'recieve_emails', 'view_signatures') VALUES (:username, :password, :salt, :email, :dob, :recieve_emails, :view_signatures)");
                    $insert->execute(array(
                        ":username" => $username,
                        ":password" => $hashedPassword,
                        ":salt" => $salt,
                        ":email" => $email,
                        ":dob" => $dob,
                        ":recieve_emails" => $recieve_emails,
                        ":view_signatures" => $view_signatures
                    ));
                    header('Location: index.php?success=registered');
                    exit();
                }
            }
        } else {
            header('Location: register.php?error=nomatchpassword');
        }
    } else {
        header('Location: register.php?error=nomatchemail');
    }
} else {
    header('Location: index.php');
}
?>

最佳答案

MySQL 的列换行是通过反引号 (`) 完成的,而不是通过单引号 (') 完成的。因此您需要更改查询语法

INSERT INTO users ('username', 'password', 'salt', 'email', 'dob', 'recieve_emails', 'view_signatures') VALUES (:username, :password, :salt, :email, :dob, :recieve_emails, :view_signatures)

INSERT INTO users (`username`, `password`, `salt`, `email`, `dob`, `recieve_emails`, `view_signatures`) VALUES (:username, :password, :salt, :email, :dob, :recieve_emails, :view_signatures)

http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

P.S.:您的代码确实有太多 if/else 嵌套 block 。考虑优化它们。阅读它可能会让人迷失方向。

关于php - PDO 插入错误 - 执行(array());,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21809501/

相关文章:

php - 切换到 PDO,提交时出错

php - 试图获取非对象的属性

java - 如何在 android 中循环 json 数组?

python - 不正确的日期时间值 : '' 2012-07-14 23:00:0 0''

PHP MySQL 如何获取在 while 循环中创建的变量

php - Swift 2,解析 JSON,空响应

php - wp.​​customize 不是一个函数

mysql - 使用聚合函数为每个数据选择一行

PHP fwrite();创建额外的角色

php - 根据类别列出子类别 - 从 mysql 拉取