【问题标题】:php Error in sql syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' ' at line 1php sql 语法错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“”附近使用正确的语法
【发布时间】:2014-07-08 17:50:58
【问题描述】:

我正在尝试验证用户在我正在制作的应用程序中输入的电子邮件和密码。要验证电子邮件和密码,我正在使用 php。目前我收到错误消息:

"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 ' ' at line 1"

这是我在 json-config.php 中的 php 代码:

<?php

$host = "localhost"; //Your database host server
$db = "dbname"; //Your database name
$user = "user"; //Your database user
$pass = "pass"; //Your password

$connection = mysql_connect($host, $user, $pass);  

if(!$connection) {

die("Database server connection failed.");  

} else {
//Attempt to select the database

$dbconnect = mysql_select_db($db, $connection);
//echo "connection working";
//Check to see if we could select the database

if(!$dbconnect) {
    die("Unable to connect to the specified database!");        
}
}

?>

这是我在 json.php 中的 php 代码:

<?php
include("json-config.php");

$email = $_POST['email'];
$password = $_POST['password'];

function validate_password($plain, $encrypted) {
    if (!empty($plain) && !empty($encrypted)) {
        // split apart the hash / salt
        $stack = explode(':', $encrypted);
        //echo "<pre>";print_r($stack);echo "</pre>";
        if (sizeof($stack) != 2)
            return false;
        if (md5($stack[1] . $plain) == $stack[0]) {
            return true;
        }
    }

return false;
}

if ($_POST) {
//gets user's info based off of a username.
$query = "SELECT customer_email, customer_password FROM rt_customer WHERE customer_email = ".$email."";
//echo $query;

//This will be the variable to determine whether or not the user's information is correct.
//we initialize it as false.
$validated_info = false;
$result = mysql_query($query) or die("Error in Selection Query <br> " . $query . "<br>" . mysql_error());

//fetching all the rows from the query
while($row = mysql_fetch_assoc($result)) {
    //if we encrypted the password, we would unencrypt it here, but in our case we just
    //compare the two passwords
    if ($this->validate_password($password, $row['customer_password'])) {
        $login_ok = true;
    }
}

// If the user logged in successfully, then we send them to the private members-only page 
// Otherwise, we display a login failed message and show the login form again 
if ($login_ok) {
    echo '{"success":1, "message":"Login successful!"}';
} else {
    echo '{"success":0, "message":"Username and/or password is invalid."}';
}
}

?>

如果您需要更多信息,请随时询问,谢谢。

【问题讨论】:

  • 您的字符串值周围缺少引号
  • 具体来说,您直接从$_POST 传递到查询中的未经处理的$email
  • 拜托,拜托,拜托....停止使用 MySQL 扩展并使用准备好的语句/绑定变量转移到 MySQLi 或 PDO
  • mysql_* 函数不再支持,它们是 officially deprecated不再维护,并将在未来。您应该使用PDOMySQLi 更新您的代码,以确保您的项目在未来的功能。

标签: php mysql sql json


【解决方案1】:

你需要在$email周围加上引号

$query = "SELECT customer_email, customer_password FROM rt_customer ".
         "   WHERE customer_email = '".$email."'";

虽然这将解决您的问题,但我建议您查看 mysqli 并让准备好的语句为您进行引用。您直接使用来自 post 数组的数据的方式使您可以进行 sql 注入。

如果你真的想保留旧的 mysql 库,使用这个:

$email = mysql_real_escape_string( $email);

在将其放入查询之前(但在建立连接之后)。

【讨论】:

  • 或只是"SELECT customer_email, customer_password FROM rt_customer WHERE customer_email = '$email'";
  • @Strawberry 是的,也可以这样做,没有连接 futz
  • 当我在查询中添加引号时,我收到来自应用程序的连接错误。错误代码:500。
  • @Jaw_khan 不应该是查询失败,但你可以试试草莓的版本来确认
  • @Ray 但我之前没有收到该错误,这可能与我的应用程序代码本身有关吗?
【解决方案2】:
1)SELECT customer_email, customer_password FROM rt_customer WHERE customer_email = '$email'"
2)SELECT customer_email, customer_password FROM rt_customer WHERE customer_email = '".$email."'"

是正确的,但遵循标准将帮助您避免意外错误, 永远记得用 vachar 类型的表字段检查条件,用单引号('字符串值')或双引号(“字符串值”)围绕它

也只能在 (database name) 或 (table name) 或 (field name) 周围使用反引号,不要对这些名称使用单引号或双引号, 您可以使用我的blog 获取更多信息。

【讨论】:

    猜你喜欢
    • 2016-03-04
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多