简体   繁体   English

使用HTML日期范围表单数据在MySQL查询中选择日期范围

[英]Use HTML daterange form data to select for a date range in MySQL query

Thank you for viewing this question. 感谢您查看此问题。 I have implemented a jquery/html datepicker form that allows users to select a start date and an end date. 我已经实现了jquery / html datepicker表单,该表单允许用户选择开始日期和结束日期。 The HTML for this form is: 此表单的HTML是:

        <form id="MyForm">
            <input type="text" class = "datepicker" name = "StartDate" id="StartDate" placeholder = "Start Date" style = "width : 85px; margin-left: 10px; margin-top: 1mm;"/>
            <br />
            <input type="text" class = "datepicker" name = "EndDate" id="EndDate" placeholder = "End Date" style = "width : 85px; margin-left: 10px;"/>
            <br />
            <input type = "submit" name = "submit" value = "Go" style = "font-weight: bold; margin-left: 10px;"/>
        </form>

The jquery is pretty simple and stores the inputted dates in the same format as the format of the dates in the MySQL table I am querying from. jQuery非常简单,它以与我查询的MySQL表中的日期格式相同的格式存储输入的日期。

I would like to create a system in this web page that allows users to dynamically alter the results displayed in terms of the selected date range. 我想在此网页中创建一个系统,该系统允许用户动态更改所选日期范围内显示的结果。 Ideally, I would like to do this in a dynamic fashion using AJAX. 理想情况下,我想使用AJAX以动态方式进行此操作。 Bear in mind that this page is already receiving a REQUEST in terms of a separate field's (project) identity. 请记住,此页面已经收到有关单独字段(项目)身份的REQUEST。 What I mean by this is that a page's URL is going to be something like ~~~~.php?project=LOW003, where LOW003 is a session variable $project. 我的意思是,页面的URL类似于~~~~ .php?project = LOW003,其中LOW003是会话变量$ project。 The date range form should alter the query below to one that selects in terms of the inputted date range: 日期范围表单应将下面的查询更改为根据输入的日期范围选择的查询:

      SELECT project, participant, reel, machine, qc_gsr, qc_hr, qc_acz, qc_bre 
          FROM rtcdb.session 
          WHERE site = 'ORL001';

I would like to turn this query, based on the date-range form inputs, to something like: 我想根据日期范围形式的输入将此查询转换为:

     SELECT project, participant, reel, machine, qc_gsr, qc_hr, qc_acz, qc_bre 
         FROM rtcdb.session 
         WHERE download_date >= '$start_date' 
         AND download_date <= '$end_date';

To sum it up, I would like to use AJAX and PHP to get input vars from an HTML form stored in the format yy-mm-dd to then alter all of my MySQL queries to select in terms of a date range. 综上所述,我想使用AJAX和PHP从以yy-mm-dd格式存储的HTML表单中获取输入变量,然后更改所有MySQL查询以选择日期范围。 Thanks for your help! 谢谢你的帮助!

I'm building an SQL for use by depreciated mysql_* functions to show meaning only: 我正在构建一个供折旧的mysql_ *函数使用的SQL,以仅显示含义:

$where=array();
if( (isset($_REQUEST['site'])) && (!empty($_REQUEST['site'])) )
{
    $where[]="site='".mysql_real_escape_string(urldecode($_REQUEST['site']))."'";
}
if( (isset($_REQUEST['start_date'])) && (!empty($_REQUEST['start_date'])) )
{
    $where[]="download_date>='".mysql_real_escape_string(urldecode($_REQUEST['start_date']))."'";
}
if( (isset($_REQUEST['end_date'])) && (!empty($_REQUEST['end_date'])) )
{
    $where[]="download_date<='".mysql_real_escape_string(urldecode($_REQUEST['end_date']))."'";
}

$sql="SELECT project, participant, reel, machine, qc_gsr, qc_hr, qc_acz, qc_bre 
FROM rtcdb.session";
if(sizeof($where)>0)
{
    $sql.=" WHERE ".implode(" AND ", $where);
}

echo $sql;

You can prepare the statement too, but it was quicker for me to explain this way. 您也可以准备该语句,但是对我来说,这样解释起来更快。
No Validation carried out as per comments 没有根据评论进行验证

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

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