空白提交值和返回的mysql查询问题

时间:2011-03-17 12:02:27

标签: php mysql easyphp

<?php
$link = mysql_connect('127.0.0.1', 'ilhan', 'password123');
if(!$link)
    {
        die('Could not connect: ' . mysql_error());
    }
mysql_select_db("metu", $link);
mysql_set_charset('utf8',$link);

if(isset($_POST["email"])) // AND strlen($_POST["email"])>1 solves the problem
//  but I didn't get why the page is redirected...
    {
        $email = mysql_real_escape_string($_POST["email"]);
        $password = mysql_real_escape_string($_POST["password"]);

        $result = mysql_query("SELECT password, id FROM users WHERE email = '$email'");

        if (!$result)
            {
                die('Invalid query: ' . mysql_error());
            }

        $row = mysql_fetch_assoc($result);

        if($row['password'] == $password) 
            {
                $_SESSION['valid_user'] = $row['id'];
                mysql_close($link);
                header("Location: http://www.example.com");
            }
        else $login = false;


        mysql_close($link);
    }
else $login = false;
?>

如果用户提交空白电子邮件和空白密码,您认为该页面会被重定向吗?它不应该,但页面被重定向。这是为什么?错误?

3 个答案:

答案 0 :(得分:2)

我一开始忽略了这个问题。 这是:

if($row['password'] == $password) 

$row['password']等于空$password。评估为true

这就是我要做的事情

<?php
include 'db.php';    
if(isset($_POST["email"]))
{
    $sql = "SELECT id FROM users WHERE email='%s' AND password='%s'";
    $id  = dbGetOne($sql,$_POST["email"],MD5($_POST["email"].$_POST["password"]));
    if($id) 
    {
        $_SESSION['valid_user'] = $row['id'];
        header("Location: http://www.example.com");
        exit;
    }
}

答案 1 :(得分:0)

  if(isset($_POST["email"])) 
 // AND strlen($_POST["email"])>1 solves the problem 
 // but I didn't get why the page is redirected...

如果$ _POST [“email”]为空字符串 - &gt; isset将返回true

编辑:添加if ( isset($_POST["email"]) && isset ($_POST["password"]) )以避免空密码,并确保{if($ row ['password'] == $ password)'

$row不为空

答案 2 :(得分:0)

使用mysql_num_rows函数查找返回的行数。 mysql_query将在出错时返回FALSE,但在您的情况下,如果电子邮件为空,则SQL仍然是有效的SQL。它只会返回一个空结果集。