大家好! 拜托,我是代码点火器的新手...实际上,我尝试使用内部连接编写搜索模块脚本,但我不知道什么是不正确的。
这是错误消息: 遇到未捕获的异常 类型:参数计数错误
消息:参数太少,无法运行 Career_model::quick_search((,在第 69 行的 C:\xampp\htdocs\hue\application\controllers\Pages.php 中传递了 0,并且正好是预期的 4
个文件名: C:\xampp\htdocs\hue\application\models\Career_model.php
行号:124
回溯:
文件: C:\xampp\htdocs\hue\application\controllers\Pages.php 行: 69 功能:quick_search
文件: C:\xampp\htdocs\hue\index.php 行: 286 功能:require_once
这是我的控制器:
function quick()
{
if ($this->session->userdata('username')):
$session_data = $this->session->userdata('username');
$query = $this->db->query("select * from account where email='$session_data'");
$data = $query->result();
foreach ($data as $key => $value);
$this->form_validation->set_rules('gender','Gender', 'required');
$this->form_validation->set_rules('from','lowest age', 'required');
$this->form_validation->set_rules('to','highest age', 'required');
if ($this->form_validation->run() == FALSE):
$this->load->view('pages/search_result');
else:
$gender = $this->input->get_post('gender');
$from = $this->input->get_post('from');
$to = $this->input->get_post('to');
$country = $value->country;
//I suppose this is where the problem is coming from
if($this->Career_model->quick_search($gender,$from,$to,$country)):
$data = $this->session->set_flashdata('error','something went wrong...');
redirect('pages/search_result',$data);
else:
$result['data'] = $this->Career_model->quick_search();
redirect('pages/search_result',$result);
endif;
endif;
endif;
}
这是模型:
function quick_search($gender,$from,$to,$country)
{
$this->db->join("about" , "account.ref_id = about.user_id");
$this->db->where('gender',$gender);
$this->db->where('country',$country);
$this->db->where('age',$from);
$this->db->where('age <=',$to);
$query = $this->db->get('account');
return $query->result();
}
提前感谢...
错误是因为在 else 条件下调用模型时没有提供任何参数(需要 4 个参数(。我已经为您编写了一个可能的解决方案,看看是否有帮助。
将此代码删除到下面的代码 -
// Remove this code
if($this->Career_model->quick_search($gender,$from,$to,$country)):
$data = $this->session->set_flashdata('error','something went wrong...');
redirect('pages/search_result',$data);
else:
$result['data'] = $this->Career_model->quick_search(); // this line is causing error because 0 parameters are passed.
redirect('pages/search_result',$result);
endif;
自
// This is probably what you want ↓↓
$result = $this->Career_model->quick_search($gender, $from, $to, $country); // get the data once and then check if data empty or not
if(!empty($result)){ // success
$data['result'] = $result;
}else{ // fail
$this->session->set_flashdata('error','No data found');
}
redirect('pages/search_result',$data);