我需要匹配在数据库中获取的用户名和密码。现在的问题是我无法匹配用户名和密码,如下图所示:
下面是我的代码:
HTML
<body>
<div class="container box">
<div class="form-group">
<h3 align="center">Live Username Available or not By using PHP Ajax Jquery</h3><br />
<label>Enter Username</label>
<input type="text" name="username" id="username" class="form-control" />
<input type="password" name="password" id="password" class="form-control" />
<span id="availability"></span>
<br /><br />
<button type="button" name="register" class="btn btn-info" id="register" disabled>Register</button>
<br />
</div>
<br />
<br />
</div>
</body>
jQuery脚本-这是在用户名和密码匹配或不匹配之后显示匹配可用性的地方
<script>
$(document).ready(function(){
$('#username, #password').blur(function(){
var username = $('#username').val();
var password = $('#password').val();
$.ajax({
url:'check.php',
method:"POST",
data:{user_name:username, password:password},
success:function(data)
{
console.log(data);
if(data != '0')
{
$('#availability').html('<span class="text-danger">Username and Password not Match</span>');
$('#register').attr("disabled", true);
}
else
{
$('#availability').html('<span class="text-success">Username and Password Available</span>');
$('#register').attr("disabled", false);
}
}
})
});
});
</script>
check.php-数据库连接和查询
<?php
//check.php
$connect = mysqli_connect("localhost", "username", "", "dbname");
if(isset($_POST["user_name"], $_POST["password"]))
{
$username = mysqli_real_escape_string($connect, $_POST["user_name"]);
$password = mysqli_real_escape_string($connect, $_POST["password"]);
$query = "SELECT * FROM admin WHERE username = '".$username."' AND password = '".$password."' ";
$result = mysqli_query($connect, $query);
echo mysqli_num_rows($result);
}
?>
答案 0 :(得分:1)
我认为您的条件在客户端脚本中是错误的。如果用户名和密码匹配,则它将返回大于0的值。在这种情况下,您的代码应为
success:function(data)
{
console.log(data);
if(data == 0)
{
$('#availability').html('<span class="text-danger">Username and Password not Match</span>');
$('#register').attr("disabled", true);
}
else
{
$('#availability').html('<span class="text-success">Username and Password Available</span>');
$('#register').attr("disabled", false);
}
}
答案 1 :(得分:0)
最低分 这是OP也存在的问题的解决方案。请参阅主线程注释。谢谢。
只需将POST值包装到md5 function中即可。
$password = md5(mysqli_real_escape_string($connect, $_POST["password"]));
或可以尝试使用MD5 in database。
$query = "SELECT * FROM admin WHERE username = '".$username."' AND password = MD5('".$password."') ";
然后,密码哈希应等于数据库哈希。
答案 2 :(得分:0)
您可以尝试以下操作:
loginCheck(){
if(this.form.valid){
const userCategory = this.form.value.userCategory;
const userName = 'admin'; //this.form.value.userName;
const passwd = 'admin'; //this.form.value.passwd;
console.log(userCategory, userName, passwd);
this._storage.set('IS_LOGGED_IN', true);
this._authService.isLoggedIn = true;
this.router.navigate(['admin']);
}
}