codeigniter不从DB中获取数据

时间:2018-06-05 05:56:49

标签: codeigniter

无法从db获取电子邮件或密码以进​​行验证。

模特:

class Login_model extends CI_Model
{

    public function login_valid($email,$password)
    {
        $q = $this->db-> select('*')
                        ->where(['email'=>'$email','password'=>'$password'])
                        ->get('admin');  
    }
 }

控制器:

class Adminlogin extends CI_controller
{

public function login_form()
{
    $this->load->view('common/login_form');
}

public function login_check()
{

$this->form_validation->set_rules('email','email','required|valid_email');
$this->form_validation->set_rules('password','password','required|min_length[8]|max_length[15]');

    if ($this->form_validation->run())
    {

        $email   = $this->input->post('email');
        $password = $this->input->post('password');

        $this->load->model('Login_model');
        $admin_id = $this->Login_model->login_valid($email,$password);

        if($this->load->model('Login_model')){
        $this->load->library('session');
        $this->session->set_userdata('Id',$admin_id);
        $this->load->view('admin/admin_dashboard');
        }
        else {
            echo "model not loaded";
        }
    }
    else
    {
        $this->load->view('common/login_form');
    }
}

以上是我的代码。我不确定它的错误在哪里。我的登录凭据是不是从数据库获取的?

1 个答案:

答案 0 :(得分:0)

你的模型应该是这样的:

public function login_valid($email,$password)
{
    $q = $this->db->get_where('admin',['email'=>$email,'password'=> $password]);
    if ($q->num_rows() > 0 )
    {
       /*here return only admin id change column name if it is not id*/
       return $q->row()->id;
    }  
}

在控制器中:

检查$admin_id是否不适用于以下代码中的型号:

注意:在控制器construct()中加载您的模型(只是建议);

    if ($this->form_validation->run())
    {

        $email   = $this->input->post('email');
        $password = $this->input->post('password');

        $this->load->model('Login_model');
        $admin_id = $this->Login_model->login_valid($email,$password);

        if($admin_id){
        $this->load->library('session');
        $this->session->set_userdata('Id',$admin_id);
        $this->load->view('admin/admin_dashboard');
        }
        else {
            echo "admin id not found";
        }
    }
    else
    {
        $this->load->view('common/login_form');
    }

更多信息:https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data