登录不会返回正确的用户ID

时间:2014-10-03 08:18:46

标签: php codeigniter

当我登录时,它不会返回属于用户名的正确user_id。相反,我收到此错误消息:

  

错误号:1054'where子句中的未知列'0'SELECT * FROM(用户)WHERE 0 ='user_id'AND 1 IS NULL文件名:C:\ Xampp \ htdocs \ codeigniter-cms \ system \ database \ DB_driver.php行号:330

我如何才能获得属于登录用户名的正确user_id

用户lib已自动加载。

<?php

class User {

    public function __construct() {
        $this->CI =& get_instance();
    }

    public function getUserID($user_id) {
        if ($this->CI->session->userdata('username')) {
            return $this->CI->db->get_where('user', array('user_id', $user_id));
        } else {
            return false;
        }
    }
}

控制器

public function index() {
    $this->document->setTitle($this->lang->line('heading_title'));

    $data['text_logged'] = sprintf($this->lang->line('text_logged'), '<strong>' . $this->user->getUsername() . '</strong>');

    $data['user_id'] = $this->user->getUserID($user_id);

    $data['logout'] = site_url('admin/common/logout');

    return $this->load->view('common/dashboard', $data);
}

2 个答案:

答案 0 :(得分:3)

我相信你的意思是:

 return $this->CI->db->get_where('user', array('user_id'=> $user_id));

您现在在数组中传递了2个值,而不仅仅是键=&gt;值对

答案 1 :(得分:0)

我现在能够正确识别用户ID。当我登录时。

我必须在我的用户库中的登录验证区域添加一个函数。

我将其添加到设置会话中,现在可以获得正确的ID。

这里添加了什么

$this->CI->db->select('user_id');
$this->CI->db->where('username', $this->CI->input->post('username'));
$query = $this->CI->db->get('user');
$row = $query->row(); 

$data = array(
'isLogged' => TRUE,
'user_id' => $row->user_id, 
'username' => $this->CI->input->post('username')
);

$this->CI->session->set_userdata($data);

用户自定义

<?php

class User {

    public function __construct() {
       $this->CI =& get_instance();
    }


   public function login() {
       $this->CI->load->library('password');

       $this->CI->db->select('password');
       $this->CI->db->where('username', $this->CI->input->post('username'));
       $query = $this->CI->db->get('user');
       $row = $query->row();

       if ($this->CI->password->validate_password($this->CI->input->post('password'), $row->password)) {

       $this->CI->db->select('user_id');
       $this->CI->db->where('username', $this->CI->input->post('username'));
       $query = $this->CI->db->get('user');
       $row = $query->row(); 

       $data = array(
       'isLogged' => TRUE,
       'user_id' => $row->user_id,
       'username' => $this->CI->input->post('username')
       );

       $this->CI->session->set_userdata($data);

       return true;

       } else {

       $this->CI->session->set_flashdata('error', 'Incorrect Username Or Password! Please Try Again');
       return false;
       }
   }

   public function logout() {
   $this->CI->session->sess_destroy();
   }

   public function getUsername() {
   return $this->CI->session->userdata('username');
   }

   public function getUserID() {
   return $this->CI->session->userdata('user_id');
   }


}