【发布时间】:2019-04-01 21:54:28
【问题描述】:
我正在尝试通过检查用户名和密码是否正确来从表 users 中获取特定行的一些数据。 我不明白为什么它不起作用!!
编辑:我插入整个页面的代码。我正在使用表单,因为我以相同的表单具有注册按钮和登录按钮。我正在检查用户按下了哪一个,然后执行操作。
$host = "xx";
$user = "xx";
$pass= "xx";
$dbname = "xx";
$conn = new mysqli($host,$user,$pass, $dbname );
if ($conn->connect_error) {
die("Connection failed : ".$conn->connect_error); //fixme
}
if ( isset($_POST['login']) ) {
if ($_POST["actionb"] == 'Login') {
$username = isset($_POST["username"]) ? $conn->real_escape_string($_POST["username"]) : "";
$password = isset($_POST["password"]) ? $conn->real_escape_string($_POST["password"]) : "";
echo $username;
echo $password;
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results=$conn->query($query);
if (mysqli_num_rows($results) > 0 ) {
$_SESSION['username'] = $username;
if ($results->num_rows > 0) {
// output data of each row
while ($row = $results->fetch_assoc()) {
$email = $row["email"];
$postalcode = $row["postalcode"];
$phonenumber = $row["phonenumber"];
}
}
$_SESSION['postalcode'] = $postalcode;
$_SESSION['phonenumber'] = $phonenumber;
$_SESSION['email'] = $email;
echo "You are now logged in";
header('location: confirm.php');
} else {
//echo "Wrong username/password combination";
}
}
}
?>
<html>
<style type ="text/css">
...
</style>
<body>
<form action="" method="post">
<div class="header">
<h1>For existing users</h1>
</div>
<input type="hidden" name="actionb" value="Login" >
<label for="username"><b>Username</b></label>
<input type="text" placeholder="Enter username" name="username" required>
<label for="password"><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="password2" required>
<div class="clearfix">
<button type="submit" name="login" value="Login">Login</button>
</div>
</form>
</body>
</html>
【问题讨论】:
-
什么不起作用?输出是什么?
-
请停止存储纯文本密码,这是一种糟糕的做法,会危及您和您的用户。
-
不断提示“错误的用户名/密码组合”; @user969068
-
因为您可能在执行查询之前忘记连接mysql?您的代码中是否有具有正确凭据的 mysqli_connect?
-
@FunkFortyNiner 非常正确,我的回答中包含了您的评论
标签: php html mysql database session