0

我是 PHP 脚本的新手,来自 Java 背景。到目前为止,这是一件微不足道的事情,对我来说已经变成了脑筋急转弯。所以这就是问题所在,我为一个变量分配了一些值,当尝试在 if/else 语句中使用该值时,该变量实际上并不拥有先前分配的值。这是代码: -

<?php
    session_start();

    $email = $_POST["Email"];   
    $password = $_POST["Password"];

    $db_username="root";
    $db_password="root";
    $database="mydb";
    $localhost = "mysql";

    $con = mysql_connect($localhost,$db_username,$db_password);
    mysql_select_db($database,$con) or die( "Unable to select database");

    $query = "select * from photobook.users where email ='$email' and password ='$password';" ;

    $result = mysql_query($query);

    $num=mysql_num_rows($result);

    if($num == 1){
        while($row = mysql_fetch_array($result))
             {
                $_SESSION['email'] = $row['email'];

                $_SESSION['username'] = $row['username'];   
             }

        header("location: home.php");

    }

    else{

        include "photoBookProtocol.php";

                print "<br>email value after photobookprotocol file include is $email";
                print "<br>password value after photobookprotocol file include is $password";

        $obj=new Protocol();

        $var = $obj->loginCheck($email,$password);

                print "value of var received is $var";

        if($var == 0){
            session_destroy();
                        print "<br>user does not exist";
            //header("location: login.php");
        }
        else{
            $_SESSION['email'] = $var[0];
            $_SESSION['username'] = $var[1];
            print "<br>user exists";
            header("location: home.php");

        }   
    }

    mysql_close($con);
?>

因此,当我在“else”子句中的 loginCheck($email, $password) 中传递 $email 和 $password 时,什么都没有传递。知道为什么会这样吗?

4

1 回答 1

1

您的变量范围没有任何问题,因此:

  • POST 数据中没有任何内容
  • 包含覆盖两个变量
  • loginCheck()正在接收正确的变量,但函数中存在错误

附带说明一下,由于您的脚本依赖于 POST 数据,因此您应该有一个条件来检查是否存在所需的数据,然后再继续:

if(isset($_POST['Email'], $_POST['Password']))
{
    // something posted
}
于 2012-11-28T08:16:38.497 回答