我必须在codeigniter中使用mysql在一个字段中搜索多个值。下面是我的代码。
在控制器中
public function vpsearch()
{
$data['info'] = $this->psearch_m->emp_search_form();
$this->load->view("employer/result",$data);
}
在模型中
public function emp_search_form()
{
$skill = $this->security->xss_clean($this->input->post('ps_skills'));
$jrole = $this->input->post('ps_jobrole'));
if ( $jrole !== NULL)
{
return $this->db->get('js_edu_details');
$this->db->like('js_skills','$skill');
}
}
在视图,即(./雇主/结果)
foreach($info->result() as $row)
{
echo $row->js_id."<br/><br/>" ;
}
但是,我正在获取"js_edu_details"表中的所有记录,而不是搜索"技能"的字段。
我哪里出错了?任何帮助 wud b 感谢,提前感谢。
尝试:
public function emp_search_form()
{
$skill = $this->security->xss_clean($this->input->post('ps_skills'));
//$skill = $this->input->post('ps_skills', true); other short way of getting the above result with `xss clean`
if ( $jrole !== NULL)
{
$this->db->like('js_skills',$skill); #remove the single quote around the `$skill`
$res = $this->db->get('js_edu_details');
echo $this->db->last_query(); #try to print the query generated
return $res;
}
}
Return
语句应位于like
语句之后
你应该像这样正确地排列代码
public function emp_search_form()
{
$ps_skills = $this->input->post('ps_skills')
$skill = $this->security->xss_clean($ps_skills);
if ( $jrole !== NULL)
{
$this->db->like('js_skills','$skill');
return $this->db->get('js_edu_details');
}
}
另外,您应该注意条件永远不会满足。它总是会给出错误undefined variable $jrole