【问题标题】:Syntax error or access violation: 1064 You have an error in your SQL syntax语法错误或访问冲突:1064 您的 SQL 语法有错误
【发布时间】:2014-04-29 13:56:27
【问题描述】:

我有以下 PHP 验证:

假设 POST 变量按预期设置在表单中。

 // Check if email input field is empty     
 if (empty($_POST["email"])) {
        $emailErr = "Email address required";
    }
    else {
    // If it is not empty, quote to prevent SQL injection and assign it to a variable
    $email = $_POST['email'];
    $safe_email = $db->quote($_POST['email']);
    }

    // same for password, but instead of quote I hash the password below
    if (empty($_POST["password"])) {
        $passwordErr = "Password required";
    }
    else {
        $password = $_POST["password"];
    }

如果没有错误: // 哈希密码 $hashed_pa​​ssword = md5($password);

// create an insert query, insert safe email and hashed password into database
$query = "INSERT INTO user VALUES ('', 0, '$safe_email', '$hashed_password')";

// perform the above query
$result = $db->query($query);

我遇到的问题是,当我在查询中使用 $safe_email 而不仅仅是 $email 时,它给了我这个错误:

致命错误:带有消息的未捕获异常“PDOException” 'SQLSTATE[42000]: 语法错误或访问冲突:1064 你有一个 SQL 语法错误;检查与您对应的手册 MySQL 服务器版本,以便在附近使用正确的语法 'leomessi@adidas.com'', 'ce0a8ad8a758c3d242d9e050c4707fb6')' 在行 1' 在 C:\wamp\www\Coursework\register.php 第 65 行

不太清楚问题出在哪里,因为我的语法似乎没问题。

这是我的数据库结构:

【问题讨论】:

  • 'leomessi@adidas.com'' 似乎没有正确引用字符串
  • 我很确定 db->quote 添加了所需的引号,而您将结果放在了额外的引号中。

标签: php mysql sql exception


【解决方案1】:

您已经使用$db->quote() 添加了引号,因此您需要将它们从$query 中删除。 访问http://pl1.php.net/manual/en/pdo.quote.php#112169并查看示例中的代码。

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 2020-03-02
    • 1970-01-01
    • 2020-05-03
    • 2020-02-15
    相关资源
    最近更新 更多