2012-11-16 158 views
0

我对SQL和PHP相当陌生。PHP登录脚本SQL错误

我想写一个简单的登录脚本。我有一个HTML文档中的表单,我已经证明在2个变量中需要正确的数据,但是我的脚本在执行SQL时失败了...

我也测试了mysqlWorkbench中的SQL,我想要的结果?

请帮忙。

这里是我的脚本:

<?PHP 

$odbc = mysql_connect('localhost', 'root', '') or die ("could not connect to database"); 
mysql_select_db('examresults', $odbc) or die("Could not find database"); 

// username and password sent from form 
$username=$_POST['username']; 
$password=$_POST['password']; 

$sql='SELECT * FROM tuser where username = '.$username.' and password = '.$password.''; 

$result = mysql_query($sql, $odbc) or die ("Error in SQL"); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

//If result matched username and password, table row must only equal 1 row 
if($count==1) 
{ 
    header("location:exammenu.php"); 
} 
else 
{ 
    echo 'username and password do not match'; 
} 
?> 
+3

如果您是新手,我建议您停止使用mysql_connect,并且(如果您熟悉),请改为使用mysqli。 http://www.php.net/manual/en/function.mysql-connect.php – Lucas

+0

我必须使用MySQL的 – Matt

+1

你什么错误? – Lucas

回答

2

注意mysql_*函数已被弃用,您不应该再使用它们。您的代码也容易受到SQL注入的影响。

使用mysql_error,而不是只打印出“在SQL错误”会给我们(和你)更详细的SQL错误消息。但最有可能的是它失败了,因为你忘记在查询中围绕你的字符串放置" "

$sql='SELECT * FROM tuser where username = "'.$username.'" and password = "'.$password.'"'; 
+0

感谢星爷香港专业教育学院一直与整数,而不是字符串,忘了你需要“”谢谢字串,很多:) – Matt

2

查询应如下:

$sql='SELECT * FROM tuser where username = "'.$username.'" and password = "'.$password.'"'; 
2

如果你真的需要使用mysql至少清理输入。还要注意$ sql变量中的引号。这应该工作(尽管未测试):

<?PHP 

$odbc = mysql_connect('localhost', 'root', '') or die ("could not connect to database"); 
mysql_select_db('examresults', $odbc) or die("Could not find database"); 

// username and password sent from form 
$username=mysql_real_escape_string($_POST['username'], $odbc); 
$password=mysql_real_escape_string($_POST['password'], $odbc); 

$sql=sprintf('SELECT * FROM tuser where username = "%s" and password = "%s"', $username, $password); 

$result = mysql_query($sql, $odbc) or die ("Error in SQL"); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

//If result matched username and password, table row must only equal 1 row 
if($count==1) 
{ 
    header("location:exammenu.php"); 
} 
else 
{ 
    echo 'username and password do not match'; 
} 

我建议使用sprintf格式化你的SQL语句,使其更容易发现这样的错误。

1

你可以试试这段代码。我认为它会正常工作。

<?PHP 

$odbc = mysql_connect('localhost', 'root', '') or die ("could not connect to database"); 
mysql_select_db('examresults', $odbc) or die("Could not find database"); 

// username and password sent from form 
$username=$_POST['username']; 
$password=$_POST['password']; 

$sql="SELECT * FROM tuser where username = '".$username."' and password = '".$password."'"; 

$result = mysql_query($sql, $odbc) or die ("Error in SQL"); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

//If result matched username and password, table row must only equal 1 row 
if($count==1) 
{ 
    header("location:exammenu.php"); 
} 
else 
{ 
    echo 'username and password do not match'; 
} 
?>