0

I'm training to build a login system and I have 2 errors:

Undefined variable

And

Call to private method Database::connect()

Database connect file:

class Database 
{
    private $host;
    private $user;
    private $password;
    private $database;

    function __construct($filename)
    {
        if(is_file($filename))
            include $filename;
        else
            throw new Exception("Error!");

        $this->host     = $host;
        $this->user     = $user;
        $this->password = $password;
        $this->database = $database; 

        $this->connect();
    }

    private function connect()
    {
        // connect to the server
        if(!mysqli_connect($this->host,$this->user, $this->password,$this->database))
            throw new Exception("Error: not connected to the server.");
    }

    function close()
    {
        mysql_close();
    }
}

login model:

class Login
{
    private $username;
    private $password;
    private $cxn;

    public function __construct($username,$password)
    {
        $this->setData($username,$password);
        $this->connectToDb();
        $this->getData();
    }

    private function setData($username,$password)
    {
        $this->username = $username;
        $this->password = $password;
    }

    private function connectToDb()
    {
        include   '../model/database.php';
        $config = "../model/config.php";
        $ok = new Database($config);
        $co = $ok->connect();
    }

    public function getData()
    {
        $query =    "SELECT * FROM admin
                    WHERE 
                    'username' = '$this->username' 
                    AND 
                    'password' = '$this->password'";
        $sql   = mysqli_query($ok,$query);

        if (mysqli_num_rows($sql) > 0)
        {
            return  TRUE;
        }
        else
        {
            throw new Exception("Error Processing Request");            
        }
    }

    public function close()
    {
        $this->cxn->close();
    }
}

login controller:

include '../view/login.php';
if (isset($_POST['sub'])) 
{
    $_POST['user'] = $username;
    $_POST['pass'] = $password;
    try 
    {
        include'../model/login.php';
        $login = new login($username,$password);    

        if ($login == TRUE) {
            session_start();
            $_SESSION['username'] = $username;
            header('Location:../view/index.php');
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
ziko310
  • 1
  • 4
  • if a method inside a class is defined as 'private' it can only be called from inside this class (or it's child-classes). That's your second error. So declare it as 'public' if you need it to be called from outside. – Jeff Jul 29 '16 at 23:45
  • undefined variable: where?? what line? please post complete error-messages. or debug your code yourself... This is not supposed to be a debugging-site. – Jeff Jul 29 '16 at 23:46
  • ok, I found it. in login controller `$_POST['user'] = $username;` should be the other way round. `$username = $_POST['user'];` – Jeff Jul 29 '16 at 23:47
  • username in C:\wamp\www\oop2\controller\login.php on line 5 – ziko310 Jul 29 '16 at 23:51
  • Undefined variable: password in C:\wamp\www\oop2\controller\login.php on line 6 – ziko310 Jul 29 '16 at 23:51
  • this took me 2 minutes to find out without knowing the code. If you are able to write such a bunch of code you should at least be able to debug such simple errors. – Jeff Jul 29 '16 at 23:51
  • and also line 10 for those tow variable – ziko310 Jul 29 '16 at 23:52
  • 1
    I answered you already. Both errors fixed. You are the one who asked for help and I gave help. No need for such a tone like "then go"! – Jeff Jul 30 '16 at 00:12

0 Answers0