从mysql数据库授权用户

时间:2010-04-19 07:08:29

标签: php mysql

我吮吸php,无法在这里找到错误。该脚本从html获取2个变量“username”和“password”,然后根据MySQL数据库检查它们。当我运行这个时,我得到以下错误“查询是空的”

<?
if ((!$_POST[username]) || (!$_POST[password])) {
    header("Location: show_login.html");
    exit;
}
$db_name = "testDB";
$table_name = "auth_users";
$connection = @mysql_connect("localhost", "admin", "pass") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$slq = "SELECT * FROM $table_name WHERE username ='$_POST[username]' AND password = password('$_POST[password]')";
$result = @mysql_query($sql, $connection) or die(mysql_error());
$num = mysql_num_rows($result);
    if ($num != 0) {
        $msg = "<p>Congratulations, you're authorised!</p>";
    } else {
        header("Location: show_login.html");
        exit;
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Secret Area</title>
</head>

<body>
<? echo "$msg"; ?>   

</body>
</html>

1 个答案:

答案 0 :(得分:3)

您已将$sql传递给mysql_query,但实际上您已将查询存储在$slq