<?php
session_start();
include("includes/db.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Login</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/login.css">
</head>
<body>
<div class="container"><!-- container starts-->
<form class="form-login" action="" method="Post"><!-- form-login starts-->
<h2 class="form-login-heading"> Admin Login</h2>
<input type="text" class="form-control" name="admin_email" placeholder="Email Address" required>
<input type="password" class="form-control" name="admin_pass" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit" name="admin_login">
Log In
</button>
</form><!-- form-login ends-->
</div><!-- container ends-->
</body>
</html>
<?php
if(isset($_POST['admin_login']))
{
$admin_email=mysqli_real_escape_string($con,$_POST['admin_email']);
$admin_pass=mysqli_real_escape_string($con,$_POST['admin_pass']);
$get_admin="select * from admins where admin_email='$admin_email' AND admin_pass=' $admin_pass'";
$run_admin=mysqli_query($con,$get_admin);
$count=mysqli_num_rows($run_admin);
if($count==1){
$_SESSION['admin_email']=$admin_email;
echo"<script>alert('You are logged in into admin panel')</script>";
echo"<script>window.open('index.php?dashboard','_self')</script>";
}
else{
echo"<script>alert('Email Or password is wrong')</script>";
}
}
?>
我的查询有问题。在我的登录面板中,当我写入存储在数据库中的电子邮件和密码时,即使条件与我使用存储在数据库中的密码和电子邮件相同,if条件也会失败,并且代码的else部分将运行。
答案 0 :(得分:0)
我刚刚注意到您的查询:
$get_admin="select * from admins where admin_email='$admin_email' AND admin_pass=' $admin_pass'";
在使用$ admin_pass变量之前有一个空格。
尝试将其调整为:
$get_admin="select * from admins where admin_email='$admin_email' AND admin_pass='$admin_pass'";
答案 1 :(得分:0)
查询中有一个空格,您可以在其中输入管理员密码:
"AND admin_pass=' $admin_pass'"
应为:
"AND admin_pass='$admin_pass'"
您应该进行一些更主要的更改,而不是修复此错误:
mysqli_real_escape_string
,这将有助于防止SQL-I与使用准备好的语句相比非常容易出错。password_hash
为您执行此操作以生成哈希:http://php.net/manual/en/function.password-hash.php和password_verify
来检查它们:http://php.net/manual/en/function.password-verify.php