Thomas W
Thomas W

Reputation: 45

Insert User ID in a Session

I'm trying to get the user ID of the current user so that I can put it in a Session for other use. I tested the query and it returns the correct values, but when I try to put these values in my "user" object I get following error!

Undefined property: stdClass::$name in C:\xampp\htdocs\Project\classes\User.class.php on line 88

LINE 88 is:

$this->Name = $obj->name;

<?php

include_once("classes/Db.class.php");

class User
{
    private $m_sName;
    private $m_sPassword;
    private $m_sEmail;
    private $m_sID;

    public function __get($p_sProperty)
    {
        switch($p_sProperty)
        {
            case "Name":
            return $this->m_sName;
            break;

            case "Password":
            return $this->m_sPassword;
            break;

            case "Email":
            return $this->m_sEmail;
            break;

            case "user_id":
            return $this->m_sID;

            default:
            throw new Exception("CANNOT GET " . $p_sProperty);
        }
    }

    public function __set($p_sProperty, $p_vValue)
    {   
        switch($p_sProperty)
        {
            case "Name":
            $this->m_sName = mysql_real_escape_string($p_vValue);
            break;

            case "Password":

                $salt = "kjfiqjifmqjfemq";
                $this->m_sPassword = md5($p_vValue.$salt);

            break;

            case "Email":
            $this->m_sEmail = mysql_real_escape_string($p_vValue);
            break;

            case "user_id":
            $this->m_sID = $p_vValue;
            break;

            default:
            throw new Exception("CANNOT SET " . $p_sProperty);
        }
    }

    public function Register()
    {
        try
        {
            $db = new db('localhost', 'root', '', 'project');
            $db->insert("user_tbl (username, password, email)", "'$this->Name','$this->Password', '$this->Email'");

        }
        catch(Exception $e)
        {
            echo $e->Message;
        }
    }

    public function CanLogin()
    {

        $db = new db('localhost', 'root', '', 'project');

        $res = $db->Select("username, ID" , "user_tbl","password = '$this->Password' and email = '$this->Email'");

        if($res->num_rows == 1)
        {
            $obj = $res->fetch_object();
            $this->Name = $obj->name;
            $this->ID = $obj->ID;
            return true;

        }else
        {
            throw new Exception('Fout bij het aanmelden.');
            return false;
        }

    }

}   

?>

Upvotes: 0

Views: 95

Answers (2)

ZorleQ
ZorleQ

Reputation: 1427

Totally agree with @MichaelBerkowski in the comment above. You are trying to refer to fields, which you are not loading.

Either change the code to $this->Name = $obj->username; or add 'name' to your query

$res = $db->Select("username, ID, name" , "user_tbl","password = '$this->Password' and email = '$this->Email'");

As for the ID

You are refering to a wrong variable name. In your setter / getter, the property name is case "user_id":, but you are trying to refer to it via $this->ID. You need to change your case option to case "ID": for this to work.

Upvotes: 0

Agustin Meriles
Agustin Meriles

Reputation: 4854

Change this line

$this->Name = $obj->name;

for

$this->Name = $obj->username;

Upvotes: 1

Related Questions