如果用户只填写一个字段,MySQL 查询将不起作用
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有多种方法可以解决此问题,但最快的方法是简单地在方法签名本身中设置默认值。
相反,
您实际上可以通过像这样设置它们来添加默认值(只是一个示例)
如果用户没有选择日期或位置, $date 和 $location 将默认为您指定的值。
现在,更简洁的方法是在让用户提交表单之前实际验证用户是否已在视图中正确填写表单。您可以使用 CodeIgniter 的表单验证 http://codeigniter.com/user_guide/libraries/form_validation.html 来完成此操作 如果表单验证运行等于 false,则不让用户运行 search_flyers($date,$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
you can actually add default values by setting them like this (just an example)
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