简体   繁体   中英

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. The HTML for this form is:

        <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.

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. Bear in mind that this page is already receiving a REQUEST in terms of a separate field's (project) identity. 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. 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. Thanks for your help!

I'm building an SQL for use by depreciated mysql_* functions to show meaning only:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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