Reputation: 61
Good day! I am trying to create a "search by date" option in my website that will retrieve the records in the database whose dates matched the users' input. I have these select boxes:
<!-- FROM -->
<select name="from_month" id="from_month">
<option selected>-- MONTH --</option>
<option value="january">January</option>
...
<option value="december">December</option>
</select>
<select name="from_day" id="from_day">
<option selected>-- DAY --</option>
<?php $i = 1;
while ($i <= 31) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<select name="from_year" id="from_year">
<option selected>-- YEAR --</option>
<?php $i = date('Y');
while ($i <= 2040) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<!-- FROM (END) -->
<!-- TO -->
<select name="to_month" id="to_month">
<option selected>-- MONTH --</option>
<option value="1">January</option>
...
<option value="12">December</option>
</select>
<select name="to_day" id="to_day">
<option selected>-- DAY --</option>
<?php $i = 1;
while ($i <= 31) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<select name="to_year" id="to_year">
<option selected>-- YEAR --</option>
<?php $i = date('Y');
while ($i <= 2040) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<!-- TO (END) -->
What I originally plan to do is get all the "from_" values and store it into a variable '$from'. The same goes for the "to_". Because my database for dates looks like this:
id | title(text) | date_submitted(date) |
1 | Lorem Ipsum | 2015-01-20 |
Any help would be appreciated on how I would accomplish this. Thank you in advance!
Upvotes: 0
Views: 443
Reputation: 2521
Since you know every part of the date, MySQL's BETWEEN
would do the trick:
SELECT * FROM db WHERE mydate BETWEEN 'from_date' AND 'to_date'
from_date and to_date are your variables, get through the form.
As @AnoopS stated, use numbers to value your month boxes.
And remember to prevent injections using mysqli or PDO with prepared statements.
Upvotes: 1