用mysql验证登录

时间:2018-03-23 17:58:31

标签: php html mysql database

如我的用户名中所述,如果它是重复的帖子,则非常需要帮助,所以很抱歉!

如果身份验证失败,我试图建立一个将我重定向到我的主页的登录页面,这样我的输入将与数据库(mysql)交叉检查,然后输出成功或错误。但结果总是显示错误。我很确定它没有进入我的第一个如果检查声明。

如下所示是我的代码:

<?php
$servername = "localhost";
$username = "read";
$password = "projecttest";
$dbname = "test-member";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$password1 = $_POST["password"];
$username1 = $_POST["username"];

$chkpassword = " SELECT password FROM member WHERE password = $password1 ";
$chkusername = " SELECT username FROM member WHERE username = $username1";

if ($conn->query($chkpassword) == TRUE ) {   
echo "successful log in"
?>
<INPUT TYPE="hidden" NAME="redirect" 
VALUE="http://localhost/IPproject_test1/home.php">
<?php
}

else if ($conn->query($chkpassword) == FALSE ) {   
echo "error";
}
$conn->close();
?>

1 个答案:

答案 0 :(得分:0)

问题是您的查询。

假设用户输入密码asdf1234。您的查询将如下所示:

SELECT password FROM member WHERE password = asdf1234

那会失败,因为MySQL认为asdf1234是一个列。如果你逃避字符串,它应该工作。

$chkpassword = "SELECT password FROM member WHERE password = '{$password}'" 

所以查询看起来像这样:

SELECT password FROM member WHERE password = 'asdf1234'

我仍然无法检查== TRUE tho,但!== null