dongsong4418 2016-09-16 08:28
浏览 70
已采纳

如何使用PHP表单数据选择MySQL日期范围

I have a PHP form which submits data in the following format:

Array ( 
    [zone] => Array ( 
        [0] => East 
    ) 
    [publication] => Array ( 
        [0] => ABP 
    ) 
    [datefilter] => 09/01/2016 - 10/15/2016
)

I use the data to construct a MySQL query as follows:

  $zone_search = $_POST['zone'];
  $zone = join("','",$zone_search);
  $publication_search = $_POST['publication'];
  $publication = join("','",$publication_search);  

  $tmp = "SELECT * FROM advertisement
          WHERE zone IN('$zone')
            AND publication IN('$publication')";

However, I want to limit the data to the two dates given in the $_POST['datefilter'] field. How do I add the date filter dates into my query?

The relevant columns in the database table are:

zone
publication
release_date
  • 写回答

2条回答 默认 最新

  • doumengbai2031 2016-09-17 08:22
    关注

    Add the following logic after the post data. Assumption date data submission follow this pattern small date - large date as per the sample posted data.

    <?
        //$_POST['datefilter'] = '09/01/2016 - 10/15/2016';
        $zone_search = $_POST['zone'];
        $zone = join("','",$zone_search);
        $publication_search = $_POST['publication'];
        $publication = join("','",$publication_search);  
    
    $dates = explode("-", $_POST['datefilter']);
    
    echo $dt1 = date('Y-m-d', strtotime(trim($dates[0])));
    echo "<br/>";
    echo $dt2 = date('Y-m-d', strtotime(trim($dates[1])));
    
    $tmp = "SELECT * FROM advertisement
          WHERE zone IN('$zone')
            AND publication IN('$publication') and release_date between $dt1 and $d2";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog