未定义的变量:在PHP中

时间:2013-10-05 13:11:49

标签: php variables pdo undefined

我是一个相当新的程序员,特别是在PHP中,因为我来自VB环境。

以下是我遇到麻烦的功能,你可以看到我已经做了不少尝试(在评论中)。我认为id会留下评论,以防我与其他尝试更接近。

我以前从未使用过PDO,因为你可以看到这个功能几乎允许用户登录。

if($temp == $_POST['password'])是问题所在。显然$ temp是未定义的,但我不明白为什么,我甚至在功能的顶部声明它确定。有人有什么想法吗?

public function load_user_data() {

        $temp;
        $sql;

        try{

            // $STH = dbHandler::$DBH->prepare("SELECT * FROM tblCustomer WHERE email = :email");
            // $STH->bindValue(':email', $this->email); 
            // $STH->execute();
            // $posts = $STH->fetch(PDO::FETCH_ASSOC); //If only fetch 1 line use just "fetch" instead of "fetchAll"
            // echo '<pre>';
            // print_r($posts);
            // echo '</pre>';

            //--------

            $STH = dbHandler::$DBH->prepare("SELECT password FROM tblCustomer WHERE email = :email");
            $STH->bindValue(':email', $_POST['usermail']); 
            $STH->setFetchMode(PDO::FETCH_ASSOC);

            while($row = $STH->fetch()) {
                $temp = $row;
            }

            //$temp = $STH->fetch(['password']);

            // while($row = $STH->fetch()) {
            //     $temp = $row['password'];
            // }

            //--------

            // $sql = "SELECT password FROM tblCustomer WHERE email = :email";
            // $stmt = $PDO->query($sql); 
            // $row = $stmt->fetchObject();
            // $temp = $row->password;

            if($temp == $_POST['password']) {

                $STH = dbHandler::$DBH->prepare("SELECT * FROM tblCustomer WHERE email = :email");
                $STH->bindValue(':email', $this->email); 
                $STH->setFetchMode(PDO::FETCH_ASSOC);

                echo("we have reached here");

                while($row = $STH->fetch()) {
                    $firstname = $row['firstName'];
                    $lastname = $row['secondName'];
                    $title = $row['title'];
                    $companyname = $row['companyName'];
                    $email = $row['email'];
                    $phone = $row['phone'];
                    $email = $row['mobile'];
                    $startdate = $row['startDate'];
                    $isauthorised = $row['isAuthorised'];
                    $accstop = $row['accStop'];
                    $stopdate = $row['stopdate'];
                }
            }
        }
        catch (PDOException $e) {
            print $e->getMessage();
        }
    }

1 个答案:

答案 0 :(得分:3)

问题在于:

        $STH = dbHandler::$DBH->prepare("SELECT password FROM tblCustomer WHERE email = :email");
        $STH->bindValue(':email', $_POST['usermail']); 
        $STH->setFetchMode(PDO::FETCH_ASSOC);

        while($row = $STH->fetch()) {
            $temp = $row;
        }

首先,您需要:

        $STH->execute();

在尝试获取行之前。

其次,如果查询不匹配任何行,则while循环将永远不会进入正文,因此不会设置$temp。由于您显然只希望从查询中获得一行,因此您不需要使用while。相反,做:

if ($temp = $STH->execute()) {
    // all the code that depends on finding a row goes here
    ...
}

在该区块内,您需要执行以下操作:

if ($temp['password'] == $_POST['password'])