codeigniter在从模型到控制器传递结果或方法时遇到问题?

时间:2013-10-01 12:59:33

标签: php codeigniter login

我正在尝试创建一个基本登录,没有错误检查或任何其他东西,以努力习惯codeigniter。下面是我的控制器类方法,我试图将模型方法的结果传回来验证用户名和密码。

public function login()
{
    if (isset($_POST['email'])) {
        $this->cdata['email'] = $_POST['email'] ;
    } else {
        $this->cdata['email'] = "";
    }

    if (isset($_POST['password'])) {
        $this->cdata['password'] = $_POST['password'];
    } else {
        $this->cdata['password'] = "";
    }

    $this->load->model("dbaccess");    
    $this->loggedin = $this->dbaccess->check_input($this->cdata['email'], $this->cdata['password']);

    if($this->loggedin == TRUE) {
        $this->load->view('carerview', $this->cdata);
    } else {
        $this->cdata['warning'] = "Check failed ! Please try again";
        $this->load->view('mainview', $this->cdata);
    }
}

我认为的帖子似乎运作正常。帖子被发送回主登录/索引,下面的方法登录(如上所示)显示我的模型类,它在我的登录方法中在控制器中调用它到目前为止只有一个方法。 check_input()

class Dbaccess extends CI_Model
{
    function __construct()
    {
       parent::__construct();
    }

    function check_input($email, $password)
    {
        $this->db->select('email');
        $this->db->from('tablename');
        $this->db->where('email', $email);
        $this->db->where('password', $password);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

当我在索引页面上点击提交时,无论如何我都会收到警告,而我无法确定问题所在。

2 个答案:

答案 0 :(得分:1)

尝试它是否有效:

function login()
    {
    if( $this->input->post( null ) ){  #check if the post array is not blank
        $this->load->model("dbaccess");    
        $this->loggedin = $this->dbaccess->check_input($this->input->post('email'),$this->input->post('password'));
    }else{
        $this->loggedin = false;
    }

    if($this->loggedin == TRUE)
    {$this->load->view('carerview',$this->cdata);}
    else
    {$this->cdata['warning']="Check failed ! Please try again";
    $this->load->view('mainview',$this->cdata);
    }
}

答案 1 :(得分:0)

你的表实际上是名为tablename吗?只是尝试调试它,在check_input函数中尝试这个并发布结果(如果需要,更改用户名/通道):

function check_input($email,$password)
{

 var_dump($email);
 var_dump($password);

 $this->db->select('email');
 $this->db->from('tablename');
 $this->db->where('email', $email);
 $this->db->where('password', $password);
 $query = $this->db->get();

 echo $this->db->last_query();

 if (!$query) {
   // if query returns null
   $msg = $this->db->_error_message();
   exit("Error: ".$msg);
 } 

 if($query->num_rows() > 0)
 {
     return  TRUE;
 }
 else
 {
     return  FALSE;
 }
}