PDA

View Full Version : Newbie question - WHERE statements


ShootingBlanks
11-16-2007, 04:45 PM
Hello. I have two fields that users can enter dates into, and then I want to pull records that fall only between those two dates (based on the "due_date" column in my table). I have the fields properly formatted for SQL date format, but is this code for the "WHERE" part of my SQL expression valid? (and if not, what should it be):

"WHERE projects.due_date > '".$_GET['date1']."' AND projects.due_date <= '"..$_GET['date2']."'"

Thanks!...

_Aerospace_Eng_
11-16-2007, 06:06 PM
Did you even try running the query yourself? Did it work? If not then there is your answer.

Try this
<?php
$date1 = $_GET['date1'];
$date2 = $_GET['date2'];
$query = "SELECT due_date FROM projects WHERE due_date > '$date1' AND due_date <= '$date2'";
$result = mysql_query($query) or die("The mysql error was: " . mysql_error() . "<br>The sql query was: " . $query);
echo "Number of results returned: " . mysql_num_rows($result);
?>

guelphdad
11-17-2007, 12:30 AM
run the query outside of php first. hard code the dates. if they bring back data then you know the problem is related to the non-sql code.

also take the time to look up mysql_Real_escape_string() in php manual. right now you are inviting injection attacks on your database.