【问题标题】:Codeigniter : Like query with empty post dataCodeigniter : 类似于使用空帖子数据的查询
【发布时间】:2021-01-22 14:31:38
【问题描述】:

我正在使用 or_like 通过表单针对发布的值查询 db。如果在下面的代码中没有一个已发布的字段为空

        $tbl_name= 'tbl_member_'.$this->session->userdata('userid');
        $this->db->select('name,email,phone');
        $this->db->from($tbl_name);
        $this->db->group_start();
        $this->db->like('name', $this->input->post('name'));
        $this->db->or_like('phone',$this->input->post('phonenumber'));
        $package= $this->input->post('package');
        $this->db->or_like('body_weight', $this->input->post('body_weight'));
        $this->db->group_end();
        $query = $this->db->get();
        return  $query->result());

否则它会打印所有记录,请指导我如何解决我的空值被忽略的问题,我尝试将我的查询包装在 php 块中,但它不起作用

        $tbl_name= 'tbl_member_'.$this->session->userdata('userid');
        $this->db->select('name,email,phone');
        $this->db->from($tbl_name);
        $this->db->group_start();
        if($this->input->post('name') && $this->input->post('name')!='' ){
            $this->db->like('name', $this->input->post('name'));
        }
        if($this->input->post('phonenumber') && $this->input->post('phonenumber')!='' ){
            $this->db->or_like('phone',$this->input->post('phonenumber'));
        }
        if($this->input->post('package') && $this->input->post('package')!='' ){
            $package= $this->input->post('package');
        }
        if($this->input->post('body_weight') && $this->input->post('body_weight')!='' ) {
            $this->db->or_like('body_weight', $this->input->post('body_weight'));
        }
        if($this->input->post('blood_group') && $this->input->post('blood_group')!='' ){
            $this->db->or_like('blood_group',$this->input->post('blood_group'));

        }
        if($this->input->post('gender') && $this->input->post('gender')!='' ){
            $this->db->or_like('gender',$this->input->post('gender'));
        }

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

谢谢

编辑

我使用下面的代码对我的问题进行了排序,并且效果很好:

         if($this->input->post()){
         $form_values= array(
         'name'         =>  $this->input->post('name'),
            'phone'         =>  $this->input->post('phonenumber'),
            'gender'        =>  $this->input->post('gender'),
            'package'       =>  $this->input->post('package'),
            'body_weight'   =>  $this->input->post('body_weight'),
            'blood_group'   => $this->input->post('blood_group'),
        );
        $data['fields']= $form_values;
        $form_values=array_filter($form_values);
        if (!empty($form_values)){
            $tbl_name= 'tbl_member_'.$this->session->userdata('userid');
            $this->db->select('id,name,phone,blood_group,gender,body_weight,package');
            $this->db->from($tbl_name);
            $this->db->like($form_values);
            $data['members'] = $this->db->get()->result();

        }

【问题讨论】:

  • 你真的有每个用户的表吗? $tbl_name= 'tbl_member_'.$this->session->userdata('userid'); 这是一个糟糕的主意。当您获得 1000 个用户或 1,000,000 个用户时会发生什么。你真的会查询 1,000,000 个表来搜索以查看用户是否存在,或者他们的密码是否正确
  • 这是一个非常特殊的场景,这就是为什么它一直这样,我理解你的担忧

标签: php sql codeigniter activerecord


【解决方案1】:

您可以尝试以下方法吗:

$tbl_name= 'tbl_member_'.$this->session->userdata('userid');
$this->db->select('name,email,phone');
$this->db->from($tbl_name);
$this->db->group_start();
if(!empty($this->input->post('name'))){
    $this->db->or_like('name', $this->input->post('name'));
}
if(!empty($this->input->post('phonenumber'))){
    $this->db->or_like('phone',$this->input->post('phonenumber'));
}
if(!empty($this->input->post('package'))){
    $package= $this->input->post('package');
}
if(!empty($this->input->post('body_weight'))) {
    $this->db->or_like('body_weight', $this->input->post('body_weight'));
}
if(!empty($this->input->post('blood_group'))){
    $this->db->or_like('blood_group',$this->input->post('blood_group'));
}
if(!empty($this->input->post('gender'))){
    $this->db->or_like('gender',$this->input->post('gender'));
}
$this->db->group_end();
$query = $this->db->get();

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
猜你喜欢
  • 1970-01-01
  • 2017-05-18
  • 2019-10-24
  • 2019-04-01
  • 1970-01-01
  • 2019-04-21
  • 2015-05-05
  • 2015-08-05
  • 2023-03-25
相关资源
最近更新 更多