0

我正在尝试获取当前用户的用户 ID,以便可以将其放入 Session 以供其他用途。我测试了查询并返回了正确的值,但是当我尝试将这些值放入我的“用户”对象中时,出现以下错误!

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

第 88 行是:

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

    }

}   

?>
4

2 回答 2

1

更改此行

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

为了

$this->Name = $obj->username;
于 2013-05-15T12:54:39.343 回答
0

完全同意上面评论中的@MichaelBerkowski。您正在尝试引用未加载的字段。

将代码更改为$this->Name = $obj->username;或在查询中添加“名称”

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

至于身份证

您指的是错误的变量名。在您的 setter/getter 中,属性名称是case "user_id":,但您正试图通过$this->ID. 您需要将您的案例选项更改case "ID":为才能正常工作。

于 2013-05-15T12:58:14.283 回答