【问题标题】:unable to pass post value to second model in code igniter无法将 post 值传递给 codeigniter 中的第二个模型
【发布时间】:2016-05-15 10:51:10
【问题描述】:

这是我的代码,我正在尝试通过数据库进行管理员访问...但似乎我的用户类型验证没有得到任何东西。我试图 print_r 它并且输出只是 array(),你认为我的错误是什么。这是我的代码..

登录控制器

public function loginValidate(){

    $this->load->library('form_validation');
    $this->load->model('login_model'); 

    $this->form_validation->set_rules('USERNAME','Username','required|trim|callback_validateCreds');
    $this->form_validation->set_rules('PASSWORD','Password','required|trim|md5');

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

        $this->index();

    }else{

        $data = array(

            'USERNAME' => $this->input->post('USERNAME'),
            'is_logged_in' => 1,
            'value' => $this->login_model->returnQuery()

            );

        $this->session->set_userdata($data);

        redirect('login/adminIndex');

        $q = $this->login_model->returnQuery();

            if ($q = '1'){

                redirect('login/adminIndex');

            }elseif ($q = '2') {

                redirect('login/userIndex');

            }else{

                redirect('login/restricted');

        }

    }
} 

public function validateCreds(){

        $this->load->library('form_validation');
        $this->load->model('login_model');

        if($this->login_model->can_log_in()){ // can_log_in() model

            return true;

        }else{

            $this->form_validation->set_message('validateCreds','Username and password did not match.');  //return message to validateCreds

            return false;
        }

    }

这是我的模型

public function can_log_in(){

    $sql ="SELECT * FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?";
    $data = array(
        'USERNAME' => $this->input->post('USERNAME'),
        'PASSWORD' => $this->input->post('PASSWORD')
        );
    $query = $this->db->query($sql, $data);

    return $query;


    if ($query->num_rows() == 1){

        return true;

    }else{

        return false;
    }

}

public function returnQuery(){


    $sql = "SELECT USERCODE FROM tbl_users where USERNAME =? AND PASSWORD = ?";
    $data = array(
        'USERNAME' => $this->input->post('USERNAME'),
        'PASSWORD' => $this->input->post('PASSWORD')
        );
    $query =  $this->db->query($sql, $data);


    return $query->result();
}

没有任何错误但返回true给

            if ($q = '1'){

                redirect('login/adminIndex');

即使它不是 = 1.. 我也尝试过 == 并取消 '' 但它只会将我引导到受限页面。

【问题讨论】:

  • = 不是if 中的比较运算符,而是赋值运算符
  • 我将其更改为 == ,它只是返回 true 为 ` if ($q == '1'){ redirect('login/adminIndex');` 我正在尝试访问其他用户这是 == '2' ...
  • print_r($this->login_model->returnQuery()); 的输出仍然只是array()
  • 您无需在模型中发布。您可以简单地从 $this->input->post() 读取数据,这也将提供该模型中的详细信息。或者您可以使用 $_POST 分配可以在另一个模型中读取的新值
  • redirect('login/adminIndex');将清除您所有的帖子数据。重定向之前的更新也是如此

标签: php sql codeigniter admin


【解决方案1】:

首先请记住,您返回的是完整数组而不是单个值,因此将模型函数更改为:

public function returnQuery(){ 
   $sql = "SELECT USERCODE FROM tbl_users where USERNAME =? AND PASSWORD = ?"; 
   $data = array( 'USERNAME' => $this->input->post('USERNAME'), 'PASSWORD' => $this->input->post('PASSWORD') ); 
   $query = $this->db->query($sql, $data); 
   $result = $query->result_array(); 

   if(count($result) > 0){
      return $result[0]["USERCODE"];
   }
   else{
      return 0;
   }
}

比比较值不赋值:

if($q == 1){
   //your redirection stuff
}
elseif($q == 2){
    //your redirection stuff
}
else{
    //your redirection stuff
}

更新 1:

您的回调函数也有问题。您在检查记录数之前使用的是return $query

修改功能

public function can_log_in(){ 
   $sql ="SELECT * FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?"; 
   $data = array( 'USERNAME' => $this->input->post('USERNAME'), 'PASSWORD' => $this->input->post('PASSWORD') ); 
   $query = $this->db->query($sql, $data); 
   //return $query;

   if ($query->num_rows() == 1)
   { 
      return true; 
   }
   else{ 
      return false; 
   } 
}

【讨论】:

    猜你喜欢
    • 2013-06-12
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    相关资源
    最近更新 更多