Hi!
I am trying to retrieve a date variable out of my MySQL database through a select-list form, but I keep getting this error:
“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\‘2012-08-02\’ AND \‘2013-12-31\’ ORDER BY project_descriptions.proj_date_final ’ at line 1”
Here is the relevant part of the query:
“WHERE project_descriptions.proj_date_final BETWEEN $value”
Here is the variable:
//Grabs the date from the dropdown list
$value=“‘2007-01-01’ AND ‘2013-12-31’”;
if(isset($_POST[‘value’])) {
$value=$_POST[‘value’];
}
Here is the select menu:
<select name=“value”>
<option value=“‘2007-01-01’ AND ‘2013-12-31’”>All Dates</option>
<option value=“‘2007-01-01’ AND ‘2007-12-31’”>2007</option>
<option value=“‘2008-01-01’ AND ‘2008-12-31’”>2008</option>
<option value=“‘2009-01-01’ AND ‘2009-12-31’”>2009</option>
<option value=“‘2010-01-01’ AND ‘2010-12-31’”>2010</option>
<option value=“‘2011-01-01’ AND ‘2011-12-31’”>2011</option>
<option value=“‘2007-01-01’ AND ‘2012-08-01’”>2012</option>
<option value=“‘2012-08-02’ AND ‘2013-12-31’”>Ongoing</option>
</select>
Essentially, the default value of $value loads just fine when the page renders, but whenever I submit the form (including the All Dates value, which is the same as the default one), I get this error message.
I echoed the query in hopes that I could find something, but it’s all good except the (\') marks. Only when I mess with the quote marks does anything change–usually for the much worse! So I am pretty sure that it is the single quote marks in the value attribute of the option tag is the snag. I tried escaping them (is that the right term?) in the attribute (e.g., value=“\‘2007-01-01\’ AND \‘2013-12-31\’”>) but, again, that just made matters worse.
What am I missing here? Is there a better way to give me a range of dates from my database?