0

这可能很简单,但我不明白为什么这不起作用..

我有一个 php 表单,您可以在其中从下拉列表中选择日期范围。我给出了字段名称日、月年和日1、月1、年1。

单击提交时,它会将您带到第二个 php 表单。这是第二种形式的代码:

<?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();
?>

有什么理由这行不通吗?

我不确定我哪里出错了。当有另一个选项以及诸如“状态”之类的日期时,它会显示结果,但是当它被取出时,我只想显示日期范围之间的所有结果,它不起作用..

这有效:

<?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();
?>

但是,当“状态”字段被取出(显然是第一种形式的串行下拉列表)时,它停止工作......

4

1 回答 1

0

1)请使用 PDO 或至少mysql_real_escape_string()$dateand上$date1

2) 确保您的表格是通过 发送的GET。如果是,POST您应该使用$_POST['...']而不是$_GET.

3) 确保您的日期格式正确。您可以使用 sprintf 执行此操作。

$date = sprintf('%04d-%02d-%02d', $year, $month, $day);
$date1 = sprintf('%04d-%02d-%02d', $year1, $month1, $day1);

4) 确保curr_timestamp是有效的日期/时间字段类型。

于 2012-10-12T08:47:45.277 回答