开发者

pass a post value from view to controller to model, and back to controller in code igniter

开发者 https://www.devze.com 2023-02-10 02:36 出处:网络
I\'m creating a login form with Codeigniter, and I have a controller that collects the inputs from the form, then I want to check to make sure what the user entered is in the database, so I\'m collect

I'm creating a login form with Codeigniter, and I have a controller that collects the inputs from the form, then I want to check to make sure what the user entered is in the database, so I'm collecting those values in the post and want to send them to the model for the database connection. Then if the results are in the database I want to send something back to the controller with a yes or no and then I can go from there. I'm kind of stuck, but this is what I have so far:

The controller:

function do_login()
{
  开发者_如何学Python  $login = $this->input->post('Login');
    $pwd = md5($this->input->post('Passwd'));


}

The Model:

function check_login()
{

    $sql = $this->db->query("SELECT * FROM members WHERE loin = '?' AND password = '?'", array(//POST stuff goes in here));
    return $sql->result();

}

I'm not sure how to pass the data to the model, and then back to the controller.

Any help would be great! Thanks!


In any MVC form POST is sending to controller (in action property in form) and controller (as the name is decribed) controls what will happend, in your case should ask database for verification via model, get response, decide what to do, and use view to display results...

so in your controller:

function do_login() {
 $login = $this->input->post('Login');     
 $pwd = md5($this->input->post('Passwd')); 
 $results = $this->...your_model_name...->chek_login( parameters as login and password would help )
 // base on results: has records or has not - do something
 // maybe display view

} 


+1 to @bensiu (I tried to up the answer but wasn't able to yet)

Also: don't forget that you need to explicitly load your model.

Reading this part of the user guide should answer most of your questions.


I would add 2 parameters to check_login and make it boolean:

function check_login($user, $password)
{

    $sql = $this->db->query("SELECT * FROM members WHERE loin = '?' AND password = '?'", array($user, $password));
    if (...) 
        return TRUE;
    else
        return FALSE;
}

Make it boolean lets the full login checking inside the controller, so that if there are more tests to do, the developper who uses the model doesn't have to know them, so it avoids errors.

For instance if you want to check that there is only one row in the database, and an internal variable to know whether login is allowed or not (just an example). Would all developer read the documentation about that variable that has to be checked before validating the login? Would that documentation be written?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号