Reputation: 65
I currently have multiple search fields within one form which the user can pick and choose which one to filter, everything works perfectly until I add the date range to it. If I add the date range alone without any other fields to filter it works so I know it is not that I am writing the query wrong. Please help me figure this out. THANKS!
This is the query that works: (Also for some reason doing SELECT * won't work, I have to type the name of all the fields I want to show)
It includes the part of the form that includes the date range.
<li class="form-line form-line-column" id="id_29">
<label class="form-label-top" id="label_29" for="input_30">
From Date:
</label>
<div id="cid_29" class="form-input-wide">
<input type="text" class="form-textbox validate" id="datepicker" name="from_date" size="10" />
</div>
</li>
<li class="form-line form-line-column" id="id_29">
<label class="form-label-top" id="label_29" for="input_8">
To Date:
</label>
<div id="cid_29" class="form-input-wide">
<input type="text" class="form-textbox validate" id="datepicker2" name="to_date" size="10" />
</div>
</li>
<?php
$sso = $_GET['sso'];
$assigned_to = $_GET['assigned_to'];
$case_status = $_GET['case_status'];
$startdate = $_GET['from_date'];
$enddate = $_GET['to_date'];
$subject = $_GET['subject'];
$sql = ("SELECT
id, sso, full_name, case_status, assigned_to, resolution,
description, updated, created, additional_notes, subject
FROM rmstable2
WHERE
sso LIKE '%$sso%' AND
case_status LIKE '%$case_status%' AND
assigned_to LIKE '%$assigned_to%' AND
subject LIKE '%$subject%'");
?>
This is the query I want:
<?php
$sql = ("SELECT
id, sso, full_name, case_status, assigned_to, resolution,
description, updated, created, additional_notes, subject
FROM rmstable2
WHERE
sso LIKE '%$sso%' AND
case_status LIKE '%$case_status%' AND
assigned_to LIKE '%$assigned_to%' AND
subject LIKE '%$subject%'
AND created >= '$startdate'
AND created <= '$enddate'");
?>
Upvotes: 1
Views: 193
Reputation: 39704
// assuming date is like YYYY-MM-DD, or you can explode the time to create an timestamp
// assuming your date field in the database is date
// you can remove FROM_UNIXTIME if your field is not date
$startdate = strtotime($_GET['from_date']);
$enddate = strtotime($_GET['to_date']);
$addQuery = "";
if(isset($startdate)){
$addQueryStart = "created >= FROM_UNIXTIME($startdate)";
}
if(isset($startdate)){
$addQueryEnd = "created <= FROM_UNIXTIME($enddate)";
}
if(isset($startdate) && isset($enddate)){
$addQuery .= "AND (".$addQueryStart." AND ".$addQueryEnd.")";
} elseif(isset($startdate) && !isset($enddate)){
$addQuery .= "AND ".$addQueryStart;
} elseif(!isset($startdate) && isset($enddate)){
$addQuery .= "AND ".$addQueryEnd;
}
$sql = "
SELECT id, sso, full_name, case_status, assigned_to, resolution, description, updated, created, additional_notes, subject
FROM rmstable2
WHERE
sso LIKE '%$sso%' AND
case_status LIKE '%$case_status%' AND
assigned_to LIKE '%$assigned_to%' AND
subject LIKE '%$subject%' ".$addQuery;
Later edit: for different situations
Upvotes: 0
Reputation: 1243
Best practice is to use echo $sql;
and to paste the generated query in phpmyadmin(if u have such)! Other helpful command is echo mysql_error()
.
In your case most probably you have to check if the date format is right (it should be YYYY-MM-DD)
Also make this
AND created >= '$startdate' AND created <= '$enddate'");
to this
AND created >= '" . $startdate . "'
AND created <= '" . $enddate . "'");
Because if...
$a=1;
echo '$a';
will output
$a
Upvotes: 0
Reputation: 263693
use BETWEEN
for that and don't forget to wrap the dates with DATE( )
$sql = ("SELECT id, sso, full_name, case_status, assigned_to,
resolution, description, updated, created,
additional_notes, subject
FROM rmstable2
WHERE sso LIKE '%$sso%' AND
case_status LIKE '%$case_status%' AND
assigned_to LIKE '%$assigned_to%' AND
subject LIKE '%$subject%' AND
DATE(created) BETWEEN DATE('$startdate') AND DATE('$enddate')");
UPDATE 1
how about changing AND
to OR
$sql = ("SELECT id, sso, full_name, case_status, assigned_to,
resolution, description, updated, created,
additional_notes, subject
FROM rmstable2
WHERE sso LIKE '%$sso%' OR
case_status LIKE '%$case_status%' OR
assigned_to LIKE '%$assigned_to%' OR
subject LIKE '%$subject%' OR
(DATE(created) BETWEEN DATE('$startdate') AND DATE('$enddate'))");
Upvotes: 2
Reputation: 189
Assuming your values are formatted correctly, you could try using DATE(created) in your final query like:
AND DATE(created) >= '$startdate' AND DATE(created) <= '$enddate'
or even use BETWEEN such as:
AND DATE(created) BETWEEN '$startdate' AND '$enddate'
Upvotes: 0