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

发布于 2024-11-05 16:55:22 字数 1508 浏览 1 评论 0原文

我正在 CodeIgniter 中进行搜索,该搜索将查询数据库。

这三个字段是“位置”(下拉列表)、“日期”(日期)和“事件类型”(下拉列表)。

我当前使用的代码是:

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;

提到的事件日期与“间隔”字段结合使用,以确定事件是否落在用户搜索的日期。

该活动还必须获得批准才能显示在搜索结果中。

问题是,如果用户只填写一个字段,则该表单将无法工作。

以前的开发人员已经编写了这段代码,我负责使其工作。

通常我会将 AND Flyers.location... 更改为 OR Flyers.location,但这意味着它将返回您指定的日期或位置的结果。

I'm working in CodeIgniter with a search from that will query the database.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最后的乘客 2024-11-12 16:55:22

有多种方法可以解决此问题,但最快的方法是简单地在方法签名本身中设置默认值。

相反,

public function search_flyers($location, $date) 

您实际上可以通过像这样设置它们来添加默认值(只是一个示例)

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

如果用户没有选择日期或位置, $date 和 $location 将默认为您指定的值。

现在,更简洁的方法是在让用户提交表单之前实际验证用户是否已在视图中正确填写表单。您可以使用 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);
    }

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.

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

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文