好的,我一直在使用mysql来与我的网站一起使用,但是在某些语法上进展不佳。我已经读过它了,但是我还是觉得自己做错了...在下图中,我定义了数据库变量,然后尝试登录到包含“ ID”,“ Username”和“ ID”列的数据库“密码”。然后,我从表单中的php中定义用户名和密码输入,并要求数据库进行比较...我是否缺少某些内容?我觉得这不是将表单中的数据与数据库中的数据进行比较。即使我输入密码错误也可以。

//Name of File: LoginCheck.php <--Called with the Login.php (which has a form on it)
//posts information to LoginCheck.php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'blah');
define('DB_PASS', 'blah');
define('DB_NAME', 'Profiles');
$con = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(!$con){
    die('Could not connect. ' . '<br/>' . 'Error: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $con);
if(!$db_selected){
    die('Could not select database: ' . DB_NAME . '<br/>' . 'Error: ' . mysql_error());
}
//defines login variables from the form.
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$login =  mysql_query("SELECT * FROM Users WHERE Username = '$username' AND Password = '$password'", $con);
if(!$login){
    echo 'Error: ' . mysql_error();
    echo "Didn't log in. Not matching database intel.";
}else{
echo "Logged in matching database intel.";
}
mysql_close($con);
?>

最佳答案

mysql_query()仅返回资源。然后,您可以使用该资源获取该数据或有关查询的更多信息。

您可以使用mysql_num_rows()来查看查询是否成功:

if(!mysql_num_rows($login)){


仅供参考,您不应以纯文本格式存储密码。那是一个巨大的安全禁忌。

Please, don't use mysql_* functions in new code。它们不再维护and are officially deprecated。看到red box吗?而是了解有关prepared statements的信息,并使用PDOMySQLi-this article将帮助您确定哪个。如果选择PDO,则here is a good tutorial

关于php - 使用mysql与我的数据库确认数据时遇到问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23002172/

10-12 12:52
查看更多