我正在为数据库创建日期和时间过滤器。仅查询将在输入字段中选择的日期到日期的结果。 我已完成编码,我认为代码中缺少某些东西,因为它显示没有找到记录
表格代码:
<form method="POST" action="result_search.php">
Filter Result By Date :
From <input type="datetime" name="fdat" value="<?php echo date('Y-m-d H:i:s'); ?>">
To <input type="datetime" name="tdat" value="<?php echo date('Y-m-d H:i:s'); ?>">
<input type="submit" name="search" value="Filter Stats">
</form>
带分页的PHP代码:
<?php
$from_dat=date($_POST['fdat']);
$to_dat=date($_POST['tdat']);
echo $from_dat;
echo "</br>".$to_dat."</br>";
$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
if ($page <= 0) $page = 1;
$per_page = 10; // Set how many records do you want to display per page.
$startpoint = ($page * $per_page) - $per_page;
$statement = "`user_data` where (`date_time_vote_1` AND `date_time_vote_2` AND `date_time_vote_3` AND `date_time_vote_4` AND `date_time_vote_5` AND `date_time_vote_6` AND `date_time_vote_7` AND `date_time_vote_8` AND `date_time_vote_9` AND `date_time_vote_10`) BETWEEN '$from_dat' AND '$to_dat' ORDER BY `poll_id` ASC"; // Change `records` according to your table name.
$results = mysqli_query($con,"SELECT * FROM {$statement} LIMIT {$startpoint} , {$per_page}") or die(mysqli_error($con));
if (mysqli_num_rows($results)!= 0) {
// displaying records.
while ($row = mysqli_fetch_assoc($results)) {
echo "<tr><td class='active'>".$row['user_pin']."</td>";
echo "<td class='info'>".$row['user_pin_entry']."</td>";
echo "<td class='success'>".$row['user_vote_1']."</br>";
echo $row['user_vote_2']."</br>";
echo $row['user_vote_3']."</br>";
echo $row['user_vote_4']."</br>";
echo $row['user_vote_5']."</br>";
echo $row['user_vote_6']."</br>";
echo $row['user_vote_7']."</br>";
echo $row['user_vote_8']."</br>";
echo $row['user_vote_9']."</br>";
echo $row['user_vote_10']."</td>";
echo "<td class='info'>".$row['ip_user_vote_1']."</br>";
echo $row['ip_user_vote_2']."</br>";
echo $row['ip_user_vote_3']."</br>";
echo $row['ip_user_vote_4']."</br>";
echo $row['ip_user_vote_5']."</br>";
echo $row['ip_user_vote_6']."</br>";
echo $row['ip_user_vote_7']."</br>";
echo $row['ip_user_vote_8']."</br>";
echo $row['ip_user_vote_9']."</br>";
echo $row['ip_user_vote_10']."</td>";
echo "<td class='success'>".$row['date_time_vote_1']."</br>";
echo $row['date_time_vote_2']."</br>";
echo $row['date_time_vote_3']."</br>";
echo $row['date_time_vote_4']."</br>";
echo $row['date_time_vote_5']."</br>";
echo $row['date_time_vote_6']."</br>";
echo $row['date_time_vote_7']."</br>";
echo $row['date_time_vote_8']."</br>";
echo $row['date_time_vote_9']."</br>";
echo $row['date_time_vote_10']."</td></tr>";
}
echo "</table>";
}
else {
echo "No records are found.";
}
// displaying paginaiton.
echo pagination($statement,$per_page,$page,$url='?');
?>
答案 0 :(得分:0)
以下部分似乎是问题
(`date_time_vote_1` AND `date_time_vote_2` AND `date_time_vote_3` AND `date_time_vote_4` AND `date_time_vote_5` AND `date_time_vote_6` AND `date_time_vote_7` AND `date_time_vote_8` AND `date_time_vote_9` AND `date_time_vote_10`) BETWEEN '$from_dat' AND '$to_dat'
,因为
SELECT ('2015-08-26' BETWEEN '2015-08-01' AND '2015-08-31') AND ('2015-08-27' BETWEEN '2015-08-01' AND '2015-08-31') AND ('2015-08-28' BETWEEN '2015-08-01' AND '2015-08-31')
返回true
,而
SELECT ('2015-08-26' AND '2015-08-27' AND '2015-08-28') BETWEEN '2015-08-01' AND '2015-08-31'
返回false