显示个人用户的表格

时间:2014-09-25 01:44:19

标签: php mysql sql database

我正在尝试使用会话查看特定用户的数据。我的所有细节都出现了 以personid = 1的admin身份登录。但是当我尝试以单独的用户身份登录时,表格为 没有显示。我认为错误在代码的IFELSE部分。 我使用连接来组合多个表。我的Where语句的语法是否正确?

  protected function selectAll() {

   try {                                                   

        $this->pdo = $this->Connect();            

         session_start();

此部分用于查看管理员的所有轮班

       if( isset($_SESSION["personid"]) && $_SESSION["personid"] === '1' ) { 
       $sql = "select * from PersonShift "                 
             . " inner join Person on Person.PersonID = PersonShift.PersonID"                 
             . " inner join Role on Role.RoleID = PersonShift.RoleID"
             . " inner join BusShift on BusShift.ShiftNo = PersonShift.ShiftNo";               

       }

在本节中,登录用户只能看到他的特定班次

        else if( isset($_SESSION["personid"]) && $_SESSION["personid"] !== '1' ) {

     $sql = "select * from PersonShift "                 
             . " inner join Person on Person.PersonID = PersonShift.PersonID"                 
             . " inner join Role on Role.RoleID = PersonShift.RoleID"
             . " inner join BusShift on BusShift.ShiftNo = PersonShift.ShiftNo"
             . "where PersonShift.PersonID = ".$_SESSION['personid'];  

        }                             

        $stmt = $this->pdo->prepare($sql);                        


        }                               

        $this->result = $stmt->fetchAll();                             

        $this->pdo = null;                        

        return $this->result;                                                

               } catch (\Exception $ex) {

               throw new \Exception($ex->getMessage());

       }                      

       }         

1 个答案:

答案 0 :(得分:0)

我在那里看到了几个问题:首先,大括号不匹配,所以代码有点不可预测;第二,我不知道你在哪里执行查询。

请试试这个:

protected function selectAll() 
{
    try {                                                   
        $this->pdo = $this->Connect();            
        session_start();

        if( isset($_SESSION["personid"]) && $_SESSION["personid"] === '1' ) 
        { 
          $sql = "select * from PersonShift "                 
             . " inner join Person on Person.PersonID = PersonShift.PersonID"                 
             . " inner join Role on Role.RoleID = PersonShift.RoleID"
             . " inner join BusShift on BusShift.ShiftNo = PersonShift.ShiftNo";               
        }
        else if( isset($_SESSION["personid"]) && $_SESSION["personid"] !== '1' ) 
        {
            $sql = "select * from PersonShift "                 
                   . " inner join Person on Person.PersonID = PersonShift.PersonID"                 
                   . " inner join Role on Role.RoleID = PersonShift.RoleID"
                   . " inner join BusShift on BusShift.ShiftNo = PersonShift.ShiftNo"
                   . "where PersonShift.PersonID = ".$_SESSION['personid'];  

        } // end if 

        $stmt = $this->pdo->prepare($sql);

        $stmt->execute(); // Execute was missing

        //} This was misplaced

        $this->result = $stmt->fetchAll();                             

        $this->pdo = null;                        

        return $this->result;                                                

    } catch (\Exception $ex) {
        throw new \Exception($ex->getMessage());
    }                      
} // end function