我一直在尝试自己解决问题,但无济于事。下面是我的代码:
检查登录.php
<?php
session_start();
// Connect to db
include('db.php');
$stmt = $conn->prepare('SELECT * FROM users WHERE username = :username AND password = :password');
$stmt->execute(array(':username' => $_POST['username'], ':password' => $_POST['password']));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$affected_rows = $stmt->rowCount();
//$stmt->setFetchMode(PDO::FETCH_ASSOC);
//$affected_rows = $stmt->fetchColumn();
$username = $_POST['username'];
$password = $_POST['password'];
/*
if($affected_rows == 1) {
/*while($affected_rows = $result){
//add the user to our session variables
$_SESSION['username'] = $username;
if($affected_rows['authority'] == 'admin'){
$_SESSION['authority'] == 'admin';
header("Location: main.php");
exit;
}
if($affected_rows['authority'] == 'doctor'){
$_SESSION['authority'] == 'doctor';
header("Location: pages/doctor/index.php");
exit;
}
if($affected_rows['authority'] == 'nurse'){
$_SESSION['authority'] == 'nurse';
header("Location: pages/nurse/index.php");
exit;
}
}
echo 'OK';
}
else if($username == '' || $password == '' ){
echo 'Blank fields!';
}
else {
print 'access is not allowed !!!';
} // end of $affected_rows
*/
if($affected_rows > 0){
echo "Yes";
}
else{
echo "No";
}
?>
索引.php
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script language="javascript">
$(document).ready(function(){
$("#loginForm").submit(function(){
$("#report").removeClass().addClass('loader').html('<img src="images/ajax-loader.gif">').fadeIn(1000);
$.post("checklogin.php",{ user_name:$('#username').val(),password:$('#password').val()},function(data){
if(data=='yes'){
$("#report").fadeTo(200,1,function(){
$(this).html('Logging in.....').addClass('log').fadeTo(900,1,function(){
document.location='main.php';
});
});
} else {
$("#report").fadeTo(200,1,function(){
$(this).html('Username or password error.').addClass('error').fadeTo(900,1);
});
}
});
return false;
});
$("#password").blur(function(){
$("#loginForm").trigger('submit');
});
});
</script>
</head>
<body>
<div class="container">
<form class="form-signin" action="checklogin.php" method="post" id="loginForm">
<h2 class="form-signin-heading">User Login</h2>
<input type="text" class="input-block-level" placeholder="Username" name="username" id="username">
<input type="password" class="input-block-level" placeholder="Password" name="password" id="password">
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<button class="btn btn-large btn-primary" type="submit" name="login" id="login">Sign in</button>
<div id="report"></div>
</form>
</div> <!-- /container -->
当我输入错误时,它会给出用户名或密码错误,但是当我输入正确时,它会给出用户名或密码错误。有任何想法吗?谢谢。