Selecting Date Range on a PHP form and displaying results from MySQL database
<p>This may be something simple but I cant understand why this wouldn't work.. </p>

<p>I have a php form where you can select a date range from drop downs. I've given the field names day, month year, and day1, month1, year1.</p>

<p>When clicking submit it takes you to a second php form. Here is the code for second form:</p>

<pre><code>&lt;?php

$username="***";
$password="***";
$database="****";

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$day = $_GET['day'];
$month = $_GET['month'];
$year = $_GET['year'];
$day1 = $_GET['day1'];
$month1 = $_GET['month1'];
$year1 = $_GET['year1'];

$date1 = "$year-$month-$day";
$date2 = "$year1-$month1-$day1";

$query = "SELECT * FROM main_stock WHERE curr_timestamp BETWEEN '$date1' AND '$date2'";

$result=mysql_query($query);

$num=mysql_num_rows($result); 

?&gt;

&lt;table border="1" cellspacing="2" cellpadding="2"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Product Description&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Category&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Master Category&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Barcode&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Status&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;TimeStamp&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;New Own&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Serial No.&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;?php
$i=0;
while ($i &lt; $num) {

$f1=mysql_result($result,$i,"product_desc");
$f2=mysql_result($result,$i,"category");
$f3=mysql_result($result,$i,"mastercategory");
$f4=mysql_result($result,$i,"barcode");
$f5=mysql_result($result,$i,"status");
$f6=mysql_result($result,$i,"curr_timestamp");
$f7=mysql_result($result,$i,"newown");
$f8=mysql_result($result,$i,"serial");
?&gt;

&lt;tr&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f1; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f2; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f3; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f4; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f5; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f6; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f7; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f8; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;


&lt;?php



$i++;
}
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";
mysql_close();
?&gt;
</code></pre>

<p>Is there any reason this wouldn't work?</p>

<p>I'm not sure where I am going wrong. It displays results when there is another option as well as the date such as 'status' but when this is taken out and I just want to display all the results between the date range it doesn't work..</p>

<p>This works:</p>

<pre><code>&lt;?php

$username="+++";
$password="+++";
$database="+++";

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$day = $_GET['day'];
$month = $_GET['month'];
$year = $_GET['year'];
$day1 = $_GET['day1'];
$month1 = $_GET['month1'];
$year1 = $_GET['year1'];
$status = $_GET['status'];

$date1 = "$year-$month-$day";
$date2 = "$year1-$month1-$day1";

$query = "SELECT * FROM main_stock WHERE status = '$status' AND curr_timestamp BETWEEN      '$date1' AND '$date2'";

$result=mysql_query($query);

$num=mysql_num_rows($result); 

?&gt;

&lt;table border="1" cellspacing="2" cellpadding="2"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Product Description&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Category&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Master Category&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Barcode&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Status&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;TimeStamp&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;New Own&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;b&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;Serial No.&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;?php
$i=0;
while ($i &lt; $num) {

$f1=mysql_result($result,$i,"product_desc");
$f2=mysql_result($result,$i,"category");
$f3=mysql_result($result,$i,"mastercategory");
$f4=mysql_result($result,$i,"barcode");
$f5=mysql_result($result,$i,"status");
$f6=mysql_result($result,$i,"curr_timestamp");
$f7=mysql_result($result,$i,"newown");
$f8=mysql_result($result,$i,"serial");
?&gt;

&lt;tr&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f1; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f2; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f3; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f4; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f5; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f6; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f7; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;font face="Arial, Helvetica, sans-serif"&gt;&lt;?php echo $f8; ?&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;


&lt;?php



$i++;
}
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";
mysql_close();
?&gt;
</code></pre>

<p>But when the 'status' field is taken out (and obviously the serial drop down in the first form) it stops working...</p>
