CURDATE()过滤器无法在搜索表单上运行

时间:2015-12-31 21:04:31

标签: php mysql

我有一个网站,我正在建立一个用户可以搜索优惠券的客户端,搜索工作,但即使我有一个日期规格,并检查优惠券是否有效,所有优惠券对于一家餐馆来说......我无法弄清楚这一点。这是我的疑问:

include('includes/insert_saved_coup.php');
include('includes/connection.php'); 
global $con;

if(isset($_SESSION['user_id'])) {

if(isset($_POST['search_sub'])) {

$term = mysqli_real_escape_string($con,$_POST['term']);

    $get_rest = "SELECT * 
                FROM menu_restaurants"; 

    $run_rest = mysqli_query($con,$get_rest); 
    $row_rest = mysqli_fetch_array($run_rest);

    $get_results = "SELECT * 
                    FROM menu_coupons
                    WHERE coupon_expire > CURDATE()
                    AND coupon_desc LIKE '%".$term."%'
                    OR coupon_rest LIKE '%".$term."%'
                    OR coupon_title LIKE '%".$term."%'
                    OR coupon_amount LIKE '%".$term."%'
                    OR coupon_tag_one LIKE '%".$term."%'
                    OR coupon_tag_two LIKE '%".$term."%'
                    OR coupon_city LIKE '%".$term."%'
                    OR coupon_state LIKE '%".$term."%'
                    AND coupon_status = 'active'";

    $run_results = mysqli_query($con,$get_results);
    while($row_results = mysqli_fetch_array($run_results)) {

coupon_expire字段是一个日期字段,coupon_status字段是varchar ...我每天都在我的phpmyadmin中运行一个cron来更新状态字段以显示已过期...但是当搜索运行时,它会显示该餐厅/城市/学期的所有优惠券即使已过期或有效。

任何想法? (我觉得这是我的查询)

1 个答案:

答案 0 :(得分:0)

问题是您混合了AND / OR条件,而AND优先于OR。尝试将WHERE条件块括起来,如下所示

                  SELECT * 
                    FROM menu_coupons
                    WHERE coupon_expire > CURDATE()
                    AND coupon_status = 'active'
                    AND ( coupon_desc LIKE '%".$term."%'
                    OR coupon_rest LIKE '%".$term."%'
                    OR coupon_title LIKE '%".$term."%'
                    OR coupon_amount LIKE '%".$term."%'
                    OR coupon_tag_one LIKE '%".$term."%'
                    OR coupon_tag_two LIKE '%".$term."%'
                    OR coupon_city LIKE '%".$term."%'
                    OR coupon_state LIKE '%".$term."%' );