我一直在努力在拉里烏爾曼的PHP和MySQL第4版login_ajax.php第15章的審查和追求部分,它說:驗證電子郵件和密碼輸入從MySQL數據庫通過PHP
修改login_ajax.php,使其使用數據庫確認登錄成功。
這是我到目前爲止已經試過,無論我做什麼,我得到的迴應似乎永遠是「不正確」,然後AJAX腳本從未登錄我
到目前爲止我的代碼:
<?php
if (isset($_GET['email'], $_GET['password'])){
$email = $_GET['email'];
$password = $_GET['password'];
// Need a valid email address:
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// must match specific values:
// This values will be gotten from a database
require('../mysqli_connect.php');
// retrieve from database
$q = "SELECT email, pass FROM users WHERE email = '$email'LIMIT 1";
// run the query
$r = @mysqli_query($dbc, $q);
//$check_email = "";
//$check_pass = "";
/*while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$check_email = $row['email'];
$check_pass = $row['pass'];
}*/ // if the email and password match those in database
//if (($email == $check_email && $password == $check_pass)) {
if (mysqli_num_rows($r) > 0) {
echo 'CORRECT';
} else {
echo 'INCORRECT';
}
mysqli_close($dbc);
/* if(($_GET['email'] == '[email protected]') && ($_GET['password'] == 'testpass')){
//Set a cookie, if you want, or start a session.
// indicate success:
echo 'CORRECT';
}else{// mismatch
echo'INCORRECT';
}*/
}else{ // invalid email
echo 'INVALID_EMAIL';
}
}else{ // missing one of the two variables
echo 'INCOMPLETE';
}
它包含我不同的嘗試。
在限制''$ email'LIMIT 1'之前需要空格';更好地使用prepare語句。'mysqli_query'前刪除'@' – Saty
從'@ mysqli_query'中刪除'@'符號,以便您可以查看您的查詢何時出現錯誤 –
另外,使用[密碼API](http://php.net/password)並停止使用簡單的測試密碼。 –