【发布时间】: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,不再维护,并将在未来。您应该使用PDO 或MySQLi 更新您的代码,以确保您的项目在未来的功能。