Hi, can anyone help with formatting a date range query in MySQL?
It's just a form to enter two dates, and a second page to run the query and show the output.
The form looks like so:
<form id="getdate" action="tracking_result.php" method="post">
<table>
<tr>
<td>
<input type="radio" value="single" checked="checked" name="type"/>
</td>
<td>
Single date
</td>
<td>
<input type="text" id="date" name="date">
</td>
</tr>
<tr>
<td>
<input type="radio" value="range" name="type"/>
</td>
<td>
Date Range
</td>
<td>
<select type="text" name="smonth">
<option value="1">January</option>
--etc--
<option value="12">December</option>
</select>
<input type="text" name="sday" size="5">
</select>
<select type="text" name="syear">
<option value="2011">2011</option>
<option value="2012">2012</option>
</select>
<br />
<select type="text" name="fmonth">
<option value="1">January</option>
--etc--
<option value="12">December</option>
</select>
<input name="fday" type="text" size="5" value="1-31"/>
</select>
<select type="text" name="fyear">
<option value="2011">2011</option>
<option value="2012">2012</option>
</select>
</td>
</tr>
<tr>
</table>
<input type="submit" value="Find data" />
<input type="reset" value="Reset form" />
</form>
and the PHP on the result page:
<?php
//data from form
$type=$_POST['type'];
$date=$_POST['date'];
$startDay=$_POST['sday'];
$startMonth=$_POST['smonth'];
$startYear=$_POST['syear'];
$finDay=$_POST['fday'];
$finMonth=$_POST['fmonth'];
$finYear=$_POST['fyear'];
//create date range dates
$startRange = $startYear."-".$startMonth."-".$startDay;
$endRange = $finYear."-".$finMonth."-".$finDay;
//and the same for humans
$startDisplay = $startDay."-".$startMonth."-".$startYear;
$endDisplay = $finDay."-".$finMonth."-".$finYear;
//Connect to MySQL
$conn = mysql_connect (etc);
mysql_select_db ("etc", $conn);
if ($type=='range') {$query=mysql_query("SELECT * FROM sessions WHERE date BETWEEN $startRange AND $endRange");
echo "Sessions between ".$startDisplay." and ".$endDisplay;
} else if ($type=='single') {
$query=mysql_query("SELECT * FROM sessions WHERE date BETWEEN '2011-01-01' AND '2011-02-01'");
echo "Sessions on ".$date;
};
$result=mysql_num_rows($query);
echo "<br />The number of sessions is: ".$result;
?>
So my difficulty is that the second option (type=single) works fine, but I can't substitute the variables.
Any clues?