简体   繁体   English

如果用户仅填写一个字段,MySQL查询将不起作用

[英]MySQL query doesn't work if user only fills in one field

I'm working in CodeIgniter with a search from that will query the database. 我正在CodeIgniter中进行搜索,该查询将查询数据库。

The three fields are "Location" (dropdown list), "Date" (date) and "Event Type" (dropdown list). 这三个字段是“位置”(下拉列表),“日期”(日期)和“事件类型”(下拉列表)。

The code I'm currently using is: 我当前使用的代码是:

public function search_flyers($location, $date) {
    $date = $this->utils_model->get_sql_date($date);
    $query = $this->db->query("SELECT *, flyers.fid FROM flyers 
                LEFT JOIN event_dates ON flyers.fid = event_dates.fid 
                WHERE ((flyers.interval='weekly' 
                    AND DATEDIFF('" . $date . "',flyers.start_date)%7=0 
                    AND '" . $date . "' <= flyers.end_date)
                  OR (flyers.interval='fornightly' 
                    AND DATEDIFF('" . $date . "',flyers.start_date)%14=0 
                    AND '" . $date . "' <= flyers.end_date)
                  OR (flyers.interval='manual' 
                    AND event_dates.date = '" . $date . "' 
                    OR flyers.start_date = '".$date."' 
                    AND '".$date."' <= flyers.end_date) AND flyers.approved = 1) 
                AND flyers.location = '".$location."' 
                GROUP BY flyers.fid 
                ORDER BY flyers.start_date ASC");

    $data['flyers'] = $query->result();
    $data['rows'] = $query->num_rows();

    return $data;

The Event Date mentioned is being used in conjunction with the "interval" field to work out if an event falls on the date the user has searched for. 所提及的事件日期与“时间间隔”字段一起使用,可以确定事件是否落在用户搜索的日期。

The event also has to be approved to be able to show in search results. 该事件也必须获得批准才能显示在搜索结果中。

The problem is the form does not work if a user only fills in one field. 问题是,如果用户仅填写一个字段,则表单将无法使用。

A previous developer has written this code, and I am charged with the task of making it work. 以前的开发人员已经编写了此代码,而我则负责使其工作。

Normally I would change the AND flyers.location... to OR flyers.location , but that means it would return the results of either the date or the location you specified. 通常,我会将AND flyers.location...更改为OR flyers.location ,但这意味着它将返回日期或您指定的位置的结果。

There are several ways to fix this problem, but the quickest is to simply set a default value in the method signature itself. 有几种方法可以解决此问题,但是最快的方法是在方法签名本身中简单地设置一个默认值。

Instead of 代替

public function search_flyers($location, $date) 

you can actually add default values by setting them like this (just an example) 您实际上可以通过这样设置默认值来添加默认值(仅作为示例)

public function search_flyers($location = 'mylocationstring', $date = strtotime("-1 week"))

If the user doesn't choose a date or a location, $date and $location will default to the values you specified. 如果用户未选择日期或位置,则$ date和$ location将默认为您指定的值。

Now the cleaner way would be to actually verify that the user has filled out the form correctly in the view before letting them submit it. 现在,更干净的方法是在让用户提交表单之前,先实际验证用户是否已在视图中正确填写了表单。 You can do this with CodeIgniter's form validation http://codeigniter.com/user_guide/libraries/form_validation.html by not letting the user run search_flyers($date,$location) if the form validation run is equal to false 您可以使用CodeIgniter的表单验证http://codeigniter.com/user_guide/libraries/form_validation.html来执行此操作,如果表单验证运行等于false,则不让用户运行search_flyers($ date,$ location)

if ($this->form_validation->run() == FALSE) 
{
   //go back to your initial view
}
else
     {
          $data['results'] = search_flyers($location,$date)     
                  $this->load->view('form success',$data);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM