Selecting Date Range on a PHP form and displaying results from MySQL database
- by Sarah HSL
This may be something simple but I cant understand why this wouldn't work..
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.
When clicking submit it takes you to a second php form. Here is the code for second form:
<?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);
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<td><b><font face="Arial, Helvetica, sans-serif">Product Description</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">Category</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">Master Category</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">Barcode</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">Status</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">TimeStamp</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">New Own</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">Serial No.</font></b></td>
</tr>
<?php
$i=0;
while ($i < $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");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f7; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f8; ?></font></td>
</tr>
<?php
$i++;
}
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
mysql_close();
?>
Is there any reason this wouldn't work?
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..
This works:
<?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);
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<td><b><font face="Arial, Helvetica, sans-serif">Product Description</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">Category</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">Master Category</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">Barcode</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">Status</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">TimeStamp</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">New Own</font></b></td>
<td><b><font face="Arial, Helvetica, sans-serif">Serial No.</font></b></td>
</tr>
<?php
$i=0;
while ($i < $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");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f7; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f8; ?></font></td>
</tr>
<?php
$i++;
}
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
mysql_close();
?>
But when the 'status' field is taken out (and obviously the serial drop down in the first form) it stops working...