Forum Moderators: coopster

Message Too Old, No Replies

Search a range of dates between two fields

time comparison

         

amoonwy33

7:54 pm on Apr 1, 2005 (gmt 0)

10+ Year Member



Hi, I need to develop a search function allow my users to view the transaction between the $FromDate and $ToDate.

Here is the form I have

<form name="search" method="post" action="search.php">
From this date:
<input name="FromDate" type="text" id="FromDate">
To this date:
<input name="ToDate" type="text" id="ToDate">
<input type="submit" name="Submit" value="Submit">
</form>

I have a column "OrderDate" in my mySQL database. It has the format of "yyyy-mm-dd". I need to do something as

$resutls=mysql_query("select * from transaction WHERE OrderDate BETWEEN $FromDate AND $ToDate" , $link);

or
$resutls=mysql_query("select * from transaction WHERE OrderDate >= $FromDate AND OrderDate <= $ToDate" , $link);

Anybody can help me on how to do this?

dreamcatcher

8:00 pm on Apr 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



amoonwy44, welcome to WebmasterWorld. :)

Is something not working?

Your first SQL query looks ok, but use apostrophes instead. Also, access the form data using $_POST. Something like this:

$resutls=mysql_query("select * from transaction WHERE OrderDate BETWEEN '".$_POST['FromDate']."' AND '".$_POST['ToDate']."'" , $link);

dc

amoonwy33

8:16 pm on Apr 1, 2005 (gmt 0)

10+ Year Member



The problem is, I got the error message as following:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in search.php on line 22

The script I wrote is:

$FromDate=$_POST['FromDate'];
$ToDate=$_POST['ToDate'];

$resutls=mysql_query("select * from transaction WHERE OrderDate BETWEEN '".$FromDate."' AND '".$ToDate."'" , $link);

while($myinfo=mysql_fetch_array($results))
{
echo $myinfo['ExamDate'];
}

amoonwy33

8:22 pm on Apr 1, 2005 (gmt 0)

10+ Year Member



Do I need to convert the $FromDate and $ToDate into timestamps?

gardenguy

9:37 pm on Apr 1, 2005 (gmt 0)

10+ Year Member



At a minimum, your query $resutls does not equal your later $results (hint: typo!)