PHP OOP Login Script Help Needed

Discussion in 'PHP' started by scottlpool2003, Oct 24, 2012.

  1. #1
    I'm writing a login script connecting to a PDO database. I've tested all input/outputs which work fine, but the actual selecting the user info from the database just doesn't work. I'm getting this error:
    Fatal error: Call to undefined function getUser() in C:\xampp\htdocs\classes\loginclass.php on line 25

    If I'm going about this the wrong way, please let me know as I'm just testing out more efficient ways of coding which is why I'm going for OOP rather than procedural.

    index.php

    <form method = "post" action="login.php">
       <label>Username</label><input type="text" name="username" id="username"><br />
       <label>Password</label><input type="password" name="password">
       <label>Password</label><input type="password" name="password2">
        <input type="submit" value="submit" name="submit">
    </form>
    PHP:
    login.php

    <?php
    //Get data from the form
    if (isset($_POST['submit'])){
        $username = $_POST['username'];
        $password = $_POST['password'];
        $password2 = $_POST['password2'];
    }
    //Include the class files
    include "classes/loginclass.php";
    //Instantiate the login function
    $login = new Login();
    //Set the password variables in the class to match the password passed from the form
    $login->password = $password;
    $login->password2 = $password2;
    //Do it!
    $login->logMeIn();
    ?>
    PHP:
    loginclass.php

    <?php
    class Login {
        //Declare variables
        public $username;
        public $password;
        public $password2;
        //Start login function
        function logMeIn(){
            //Check if passwords match
            if ($this->password == $this->password2){ //Passwords match, now include db.php and check if connection to database is successful
                include ("classes/db.php");
                if ($success = true){
                    //echo "Connected to db via PDO...";
                    include ("classes/userclass.php");
                    $user = new getUser();
                    $user->table = "users";
                    $user->username = $username;
                    $user->where = "username";
                    //Do it!
                    getUserDetails();
                }else {
                    die("Not connected to the database");
                }
            }else {
                die("Your passwords do not match");
            }
        }
    }
    ?>
    PHP:
    db.php

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors','1');
    $username = "***";
    $password = "***";
    $id = 1;
    try {
        $conn = new PDO('mysql:host=localhost;dbname=user', $username, $password);
        $success = true;
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    ?>
    PHP:
    userclass.php

    <?php
    class getUser {
        //Declare variables
        public $username;
        //Get user details from database
        function getUserDetails(){
            include ("classes/db.php");
            $stmt = $conn->prepare('SELECT * FROM $this->table WHERE $this->where = :$this->$username');
            $stmt->execute(array('username' => $username));
            while($row = $stmt->fetch()) {
                print_r($row);
            }
        }
    }
    ?>
    PHP:
     
    scottlpool2003, Oct 24, 2012 IP
  2. pixelator

    pixelator Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you tried to call getUser method that does not exist in your getUser class. What you have there is just getUserDetails.
     
    pixelator, Oct 24, 2012 IP
  3. jadexe

    jadexe Member

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    26
    #3
    Replace
    getUserDetails();
    with
    $user->getUserDetails();
     
    jadexe, Oct 24, 2012 IP
  4. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #4
    Thanks, worked a treat.
     
    scottlpool2003, Oct 25, 2012 IP