2013-05-15 3 views
0

다른 사용자를 위해 세션에 넣을 수 있도록 현재 사용자의 사용자 ID를 가져 오려고합니다. 쿼리를 테스트하고 올바른 값을 반환하지만 내 "사용자"개체에 이러한 값을 넣으려고하면 다음 오류가 발생합니다!세션에 사용자 ID 삽입

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

LINE (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; 
     } 

    } 

} 

?> 
+1

귀하의 검색어는'사용자 이름, ID'를 선택합니다. 'fetch_object()'에 의해 검색된 속성은'$ obj-> username'이 될 것입니다. –

답변

0

위의 설명에서 @MichaelBerkowski와 완전히 동의합니다. 로드하지 않는 필드를 참조하려고합니다.

어느

$this->Name = $obj->username;에 코드를 변경하거나 쿼리 당신은 잘못된 변수 이름으로 다스 려하는 ID

에 관해서는

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

에 '이름'을 추가합니다. setter/getter에서 속성 이름은 case "user_id":이지만 $this->ID을 통해 참조하려고합니다. 이 기능을 사용하려면 케이스 옵션을 case "ID":으로 변경해야합니다.

+0

괜찮습니다. 도움을 주셔서 감사합니다. –

1

변경이 줄을

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

$this->Name = $obj->username; 
에 대한3210
+0

그래, 문제의 그 고정 부분은 당신에게 감사한다. 그래도 ID를 설정하지 못하게됩니다 ... "CAN NOT SET ID"는 내가 얻는 오류입니다 –