有关使用预备语句进行Ajax登录系统的问题

时间:2015-10-15 23:35:30

标签: php mysqli prepared-statement

通过使用以下Ajax和MySQLi准备语句

,我无法获得登录结果
var login = $.ajax({
    type: "POST",
    url : "assets/app/users.php",
    cache: false,
    data: data,
    beforeSend: function() {
        console.log(data);
    },
    success: function() {}
});

login.done(function(html) {
    if(html=='true'){
        window.location.replace('app.php');
    }
    else  {
        $("#loginRequest").before('<div class="alert alert-danger err" role="alert">Email or Password Is Not Correct</div>');
    }
}); 

这是服务器端的PHP我hvae

$email = $_POST['email'];
$pass  = $_POST['pass'];
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);                
$sql = "SELECT id, email, fname, lname, type FROM users WHERE `email`=? AND `pass`=?";              
$stmt = $conn->prepare($sql);
$stmt->bind_param('ss', $email,$pass);
$stmt->execute();
$stmt->bind_result($theId,$theEmail,$theFName,$theLname,$theType);
if($stmt->num_rows > 0){
    $stmt->fetch();
    echo 'true';
    $_SESSION['LOGIN_STATUS'] = true;
    $_SESSION['fname'] = $theFName;
}
else{
    echo 'false';
}

我正在将数据正确地发送到服务器,因为我已经在beforeSend(){}$_POST['email'];上测试了它们,它们都返回了内部值。我还使用正常运行的相同SQL查询来测试数据库,但我在控制台中获得有效和无效凭据的false

1 个答案:

答案 0 :(得分:1)

我认为您需要先致电$stmt->fetch()才能使用true

或者,您可以调用null并测试其结果。如果有可用行,则会返回if ($stmt->fetch()) { echo 'true'; $_SESSION['LOGIN_STATUS'] = true; $_SESSION['fname'] = $theFName; } else { echo 'false'; } static PopupWindow DebugScreen( Activity a ) { final PopupWindow popUp = new PopupWindow( a ); final View cv = a.getWindow().getDecorView().findViewById( android.R.id.content ); FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.TOP ); FrameLayout fl = new FrameLayout( a ); fl.setLayoutParams(p); final ScrollView sv = new ScrollView( a ); final TextView tv = new TextView( a ); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (cv.getHeight() * 0.9), Gravity.TOP); tv.setText("Hello World"); tv.setMovementMethod(LinkMovementMethod.getInstance()); tv.setLayoutParams( lp ); sv.addView(tv, lp); if ( false ) { final Button b; b = new Button(a); b.setText("abc123"); b.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); fl.addView(b); } popUp.setContentView(fl); popUp.showAtLocation(cv, Gravity.BOTTOM, 0, 0); popUp.setFocusable(false); popUp.setOutsideTouchable(true); popUp.setTouchable(true); popUp.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if ( event.getAction() == MotionEvent.ACTION_OUTSIDE) { popUp.dismiss(); return true; } return false; } }); popUp.update( 0, 0, (int) (cv.getWidth() * 0.9), cv.getHeight() ); return( popUp ); } 到达结果的末尾。

.row