简体   繁体   English

如果不起作用

[英]if else is not working

The model: 该模型:

function validate()
    {   
        $this->db->where('username',$this->input->post('username'));
        $this->db->where('password',md5($this->input->post('password')));
        $query = $this->db->get('memberships');
        if($query->num_rows() == 1)
        {
            return TRUE;
        }
    }
    function validate_admin()
    {   
        $this->db->where('adminname',$this->input->post('username'));
        $this->db->where('password',md5($this->input->post('password')));
        $query = $this->db->get('admin');
        if($query->num_rows() == 1)
        {
            return TRUE;
        }   
    }

The controller 控制器

function validate_credentials()
    {
    $this->load->model('membership_model');

        if($this->membership_model->validate())
        {
        $this->db->where('username',$this->input->post('username'));
        $get_profile_info = $this->db->get('memberships');
        if($get_profile_info->num_rows() > 0){
            foreach ($get_profile_info->result() as $row)
        {
            $info = array('firstname' => $row->firstname,
                            'lastname' => $row->lastname,
                            'email_address' => $row->email_address
                            );
        }
            $data = array(  
                'username' => $this->input->post('username'),
                'password' => $this->input->post('password'),
                'firstname' => $info['firstname'],
                'lastname' => $info['lastname'],
                'email_address' => $info['email_address'],
                'is_logged_in' => true
                );  
            $this->session->set_userdata($data);
            redirect('/site/main_menu');
        }}
        else if($this->membership_model->validate_admin())
        {
            echo "admin";
        }
        else
        {
            redirect('login');
        }
    }

The if else if statement is not working correctly. if else if语句无法正常工作。 The program test the first condition and if it returns false skips the second condition even if that is TRUE and execute the else statement. 程序测试第一个条件,如果返回false,则跳过第二个条件,即使该条件为TRUE并执行else语句。 I'm not sure what is going worng here. 我不确定这里会发生什么。

  • Refactor your one controller method into different methods - one for Members and one for Admin to start. 将您的一种控制器方法重构为不同的方法-一种用于“成员”,另一种用于“管理员”启动。 And because you are calling separate database tables would suggest having a separate model for each. 并且由于您要调用单独的数据库表,因此建议为每个表使用单独的模型。
  • Because you are interacting with a database table getting the profile information should happen in a model (not the controller). 因为您正在与数据库表进行交互,所以获取概要文件信息应该发生在模型中(而不是控制器)。
  • This is a personal preference but i would set the session data in a model as well. 这是个人喜好,但我也会在模型中设置会话数据。 Also there might be issues with your foreach and then getting the value $info['first name']. 另外,您的foreach可能会出现问题,然后获取值$ info ['first'']。
  • Validate the form data first before sending to your database. 发送到数据库之前,请先验证表单数据。 Its safer and its better for your users - if they forget to put in the password you want to show them the form again with a helpful message. 它对您的用户来说更安全,更好-如果他们忘记输入密码,则希望通过有用的信息再次向他们显示该表单。 http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

and remember when in doubt -- echo it out. 并记得有疑问时-请将其回声。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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