1

I'm a fairly new programmer, especially in PHP as i have come from a VB environment.

Below is the function I am having troubles with, as you can see i have had quite a few attempts (in comments). I thought id leave the comments there in case i'm closer with my other attempts.

I have never used PDO before and as you can see this function pretty much allows the user to log in.

The line if($temp == $_POST['password']) is where the problem is. Apparently $temp is undefined, but i cannot see why, i have even declared it at the top of the function to be sure. Anyone have any ideas?

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 Answer 1

3

The problem is here:

        $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;
        }

First, you need to do:

        $STH->execute();

before you try to fetch rows.

Second, if the query doesn't match any rows, your while loop will never go into the body, so $temp will not be set. Since you apparently only expect to get one row from the query, you don't need to use while. Instead, do:

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

Inside that block, you'll need to do:

if ($temp['password'] == $_POST['password'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.