Codeigniter表单验证始终返回False 3

时间:2014-05-21 18:35:57

标签: php codeigniter validation

我正在使用codeigniter,我在同一个控制器中进行了验证,一个正常,但另一个没有。这是一个不起作用的人。请帮助我暂时坚持这一段时间......

 public function validate_login() {
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<span class=error>', '</span>'); 
    $this->form_validation->set_rules('shopname','Shop Name','trim|required');
    $this->form_validation->set_rules('urname', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');

          if($this->form_validation->run() == FALSE) {
            echo"no";
            $this->login();

        }
        else {
        redirect('/user/demo','refresh');


        }

}

public function check_database($password) {
     $this->load->model('user_model');
    $shop = $this->input->post('shopname');
    $user = $this->input->post('uname');
    $result = $this->user_model->check_login($shop, $user, $password);

if($result)
{
  $sess_array = array();
  foreach($result as $row)
  {
    $sess_array = array(
      'owner_id' => $row->owner_id,
      'shopname' => $row->shopname,
      'username' => $row->username
    );
    $this->session->set_userdata('logged_in', $sess_array);
  }
  return TRUE;
}
else
{
  $this->form_validation->set_message('check_database', 'Invalid username or password');
  return FALSE;
}

以下是模型:

public function check_login($shopname, $username, $password) {
    $this->db->select('owner_id, shopname, username, password');
    $this->db->from('Owner');
    $this->db->where('shopname = ' . "'" . $shopname . "'");
    $this->db->where('username = ' . "'" . $username . "'"); 
    $this->db->where('password = ' . "'" . MD5($password) . "'"); 
    $this->db->limit(1);

    $query = $this->db->get();

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


  }

}

1 个答案:

答案 0 :(得分:0)

你的拼写不同

这里你称之为 urname

 $this->form_validation->set_rules('urname', 'Username', 'trim|required');

这里的 uname

$user = $this->input->post('uname');