繁体   English   中英

SQL:用户名和密码无法正确识别

[英]SQL: username and password not recognized correctly

..我无法弄清楚确切的问题是什么,和/或解释者为什么抱怨这个问题。 我将发布我的代码:

<?php

    $path = realpath(dirname(__FILE__));

    require("../data/userhandler.class.php");

    $db = connectViaPdo();
    //var_dump($db);

    $userHandler = new UserHandler($db);

    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    $ip = $_SERVER['REMOTE_ADDR'];

    $query = "select username, password from users where ip = {$ip}";
    $stmt = $db->prepare($query);
    $result = $stmt->fetch();

    if(!$result)
    {
        $isBanned = $userHandler->checkIfBanned($ip);

        if (!$isBanned)
        {
            $query = "INSERT INTO users(username, password, ip, email) VALUES({$username}, {$password}, {$ip}, {$email})";
            $stmt = $db->prepare($query);
            $result = $stmt->execute();
        }
        else
        {
            echo "You are BANNED from this server. Leave now and do not come back.";
            exit();
        }   
    }
    else
    {
        $query = "UPDATE users SET username = {$username}, password = {$password}, email = {$email} WHERE ip = {$ip} ";
        $stmt = $db->prepare($query);
        $result = $stmt->execute();
    }


?>

我的输出类似于以下内容:

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 '.193.239, test@email.com)' at line 1' in /home/someuser/somedomain.com/portfolio/private_html/modules/register_user.script.php:29 Stack trace: #0 /home/someuser/somedomain.com/portfolio/private_html/modules/register_user.script.php(29): PDOStatement->execute() #1 {main} thrown in /home/someuser/somedomain.com/portfolio/private_html/modules/register_user.script.php on line 29

尽管我确实遵循了Stacktrace,但是它似乎并没有真正的帮助:我的PDO声明是正确的。

我认为您的字符串值必须在引号内:

$query = "INSERT INTO users(username, password, ip, email) VALUES('{$username}', '{$password}', '{$ip}', '{$email}')";

暂无
暂无

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

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