简体   繁体   中英

having trouble in codeigniter passing result or method from model to controller?

I am trying to create a basic login, no error checking or anything yet, in an effort to get used to codeigniter. Below is my controller class method that I am attempting to pass the result from my model method back in to verify username and password.

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);
    }
}

The post from my view seems to be working fine. The post is sent back to the main login/index, to the method login (shown above) below shows my model class which is called in my login method in the controller it only has one method so far. 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;
        }
    }
}

When I hit submit on my index page I keep getting the warning no matter what and I can't figure out where the issue is.

Try if it works:

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);
    }
}

Is your table actually called tablename? Just to try and debug it, try this inside your check_input function and post the results (changing the username/pass if needed):

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;
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM