2011-06-27 68 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)

相关问题