2011-06-27 69 views
1

所以我有一個用戶名和密碼形式發佈數據到這個process.php文件。但是,我的代碼中的陷阱錯誤提示mysql查詢出了問題:「用戶名和密碼不匹配,我們會在兩秒內將您帶回登錄頁面。」PHP的幫助,不知道爲什麼這不起作用

md5工作正常,我檢查了數據庫表中的所有行都在那裏以及它應該如何。有什麼建議麼?謝謝戴夫。

<?php 

//Code DavidMaitland.me 2011 
//Process data from the login form 

require('database.php'); 

$username = mysql_real_escape_string($_POST["username"]); 
$password = mysql_real_escape_string(md5($_POST["password"])); 

$client_query = mysql_query("SELECT * FROM clients WHERE client_username='$username' and client_password='$password'") or die (mysql_error()); 
$client_data = mysql_fetch_array($client_query, MYSQL_ASSOC); 

if(mysql_num_rows($client_data) == 1) { 

    session_start(); 
    $_SESSION['client_id'] = $client_data['id']; 

    header('Location: account.php'); 

} else { 

    echo "The username and password don't match. We will take you back to the login page in two seconds."; 
    header('Refresh: 2; url=index.php'); 
} 

?> 

回答

6

改變這一行:

if(mysql_num_rows($client_data) == 1) {

if(mysql_num_rows($client_query) == 1) {

mysql_num_rows()需要mysql_query()調用的直接結果。

1

你會想要使用mysql_num_rows($client_query)而不是mysql_num_rows($client_data)

1

更改這一部分

if(mysql_num_rows($client_query) == 1) { 

    session_start(); 
    $_SESSION['client_id'] = $client_data['id']; 

    header('Location: account.php'); 

} else { 

    echo "The username and password don't match. We will take you back to the login page in two seconds."; 
    header('Refresh: 2; url=index.php'); 
} 

既然你是通過mysql_num_rows已取得的陣列上計數行的金額,則應該算它的資源($ client_query)

相關問題