2

我有六个下拉菜单用于选择两个日期我有以下代码,无论我选择哪个日期,它都不会给我任何结果

$year1 = $_POST['year1'];
$month1 = $_POST['month1'];
$day1 = $_POST['day1'];
$date1 = $year1 . "/" . $month1 . "/" . $day1;
$year2 = $_POST['year2'];
$month2 = $_POST['month2'];
$day2 = $_POST['day2'];
$date2 = $year2 . "/" . $month2 . "/" . $day2;
$result = mysql_query("SELECT * FROM services WHERE date between  '%" . $date1 . "%' AND '%" . $date2 . "%' ORDER BY id " );

但是如果我用特定日期替换最后一行中的变量 $date1 和 $date2 我会得到正确的结果。

$result = mysql_query("SELECT * FROM services WHERE date between  '2012/10/01' AND '2012/11/12' ORDER BY id " );

谁能告诉变量 $date1 和 $date2 有什么问题?

4

2 回答 2

4

您需要%从查询中删除符号并使用

$result = mysql_query("SELECT * FROM services WHERE date between  '" . $date1 . "' AND '" . $date2 . "' ORDER BY id " );
于 2012-11-12T09:32:18.663 回答
0

% 用于通配符搜索...您必须将其从代码中删除...

$sql = "SELECT * FROM services WHERE date between  '" . $date1 . "' AND '" . $date2 . "' ORDER BY id ";
mysql_query($sql);
于 2012-11-12T09:35:59.700 回答