使用会话数据进行条件检查的用户ID在codeigniter中不起作用

时间:2017-07-21 08:27:41

标签: php mysql codeigniter session

尝试使用foreach从数据库中获取一些数据。但那里的条件不起作用。

我需要通过检查tbl_cart中user_id的值来显示来自表tbl_cart的内容,这是对存储的会话数据id的意义

  

表用户   enter image description here

     

表tbl_cart

enter image description here

  

登录数据(会话数据)请查看此处以了解我如何设置会话

       public function login(){
        $data = array();
        if($this->session->userdata('success_msg')){
        $data['success_msg'] = $this->session->userdata('success_msg');
        $this->session->unset_userdata('success_msg');
    }
    if($this->session->userdata('error_msg')){
        $data['error_msg'] = $this->session->userdata('error_msg');
        $this->session->unset_userdata('error_msg');
    }
            if($this->input->post('loginSubmit')){
              $this->form_validation->set_rules('email', 'Email', 
                'required|valid_email');
              $this->form_validation->set_rules('password', 'password', 
              'required');
        if ($this->form_validation->run() == true) {
            $con['returnType'] = 'single';
            $con['conditions'] = array(
                'email'=>$this->input->post('email'),
                'password' => md5($this->input->post('password')),
                'status' => '1'
            );
            $checkLogin = $this->user->getRows($con);
            if($checkLogin){
                $this->session->set_userdata('isUserLoggedIn',TRUE);
                $this->session->set_userdata('userId',$checkLogin['id']);
                $this->session-
            >set_userdata('userName',$checkLogin['name']);
                 $this->session-
            >set_userdata('UserEmail',$checkLogin['email']);
                // redirect('users/account/');
                 redirect('users/one/');

            }else{
                $data['error_msg'] = 'Wrong email or password, please try 
 again.';
            }
        }
  

控制器

      public function oneusercart(){
        $data = array();
         if($this->session->userdata('isUserLoggedIn')){
        $data['user'] = $this->user->getRows(array('id'=>$this->session-
        >userdata('userId')));
        //load the view
        $id = $this->session->userdata('id');
        $query = $this->db->query('SELECT username,useremail FROM tbl_cart 
       where user_id= $this->user->getRows(array('id'=>$this->session-
       >userdata('userId')))' );


      $resultdata['results'] = $query->result_array();


     $this->load->view('one/home_comman_page/head');
    $this->load->view('one/usercart', $resultdata);
    $this->load->view('one/home_comman_page/footer');
    $this->load->view('one/home_comman_page/script');
   }else{

        redirect('users/login');
    }
  }    

1 个答案:

答案 0 :(得分:2)

我认为$ query会是这样的,请试试这个

  $query = $this->db->query('SELECT username,useremail FROM tbl_cart 
   where user_id= '.$this->session->userdata('userId'));