首先,我的选择选项正在运行,但是当我提交时,我的输入字段无效。我正在使用if else if语句根据选项查询我的数据库。
我怀疑这部分不起作用:
elseif ($option == "") {
$start_date = $_POST['start'];
$end_date = $_POST['end'];
$start = DateTime::createFromFormat('d-m-Y', $start_date);
$end = DateTime::createFromFormat('d-m-Y', $end_date);
$query = $db->prepare("SELECT * FROM log WHERE log_datetime >= :start AND log_datetime < :end + INTERVAL 1 DAY;");
$success = $query->execute(array(':start'=>$start->format('Y-m-d'), ':end'=>$end->format('Y-m-d')));
} else {
echo "No Data Input";
}
但是我确定我的查询肯定有效,因为我在一个单独的文件中进行了测试,但是当我将选项和输入结合使用时,输入无法正常工作并自动转到today
选项,这是第一个{{ 1}}。
这是我的表单代码:
if statement
这是我的所有result.php php代码,不包括foreach:
<form method="post" action="result.php">
<select name="report">
<option id="">Select Report Time</option>
<option id="today">Today</option>
<option id="yesterday">Yesterday</option>
<option id="thisweek">This Week</option>
<option id="thismonth">This Month</option>
<option id="thisyear">This Year</option>
<option id="lastweek">Last Week</option>
<option id="lastmonth">Last Month</option>
<option id="lastyear">Last Year</option>
</select><br />
From: <input type="text" name="start" /><br />
End: <input type="text" name="end" /><br />
<input type="submit" value="Go" />
</form>
我可以知道哪里出错了吗?对不起我的乱码,我还在学习,我正在使用SO和php手册作为参考,但对于这个问题我搜索谷歌和SO高低,但我找不到可以帮助我和我的确切问题我不得不问这个问题。
感谢所有帮助并花时间阅读此问题的人。 谢谢。
答案 0 :(得分:1)
您的直接问题是由于您的请求中没有$_POST['option']
,您应该检查$_POST['report']
。
现在我建议做些改进。
首先更改html表单中选择控件的标记:
value
属性Custom
,让用户更明显地选择自己的日期据说可能看起来像
<select name='report'>
<option value=''>Select Report Time</option>
<option value='today'>Today</option>
<option value='yesterday'>Yesterday</option>
<option value='thisweek'>This Week</option>
<option value='lastweek'>Last Week</option>
<option value='thismonth'>This Month</option>
<option value='lastmonth'>Last Month</option>
<option value='thisyear'>This Year</option>
<option value='lastyear'>Last Year</option>
<option value='custom'>Custom</option>
</select>
UPDATE2:其次您可以检查请求参数并按以下一致方式生成开始日期和日期。
$periods = array(
'today' => array('start' => 'now', 'end' => 'now'),
'yesterday' => array('start' => 'yesterday', 'end' => 'yesterday'),
'thisweek' => array('start' => 'monday this week', 'end' => 'sunday this week'),
'lastweek' => array('start' => 'monday last week', 'end' => 'sunday last week'),
'thismonth' => array('start' => 'first day of this month', 'end' => 'last day of this month'),
'lastmonth' => array('start' => 'first day of last month', 'end' => 'last day of last month'),
'thisyear' => array('start' => 'first day of january this year', 'end' => 'last day of december this year'),
'lastyear' => array('start' => 'first day of january last year', 'end' => 'last day of december last year')
);
if (isset($_POST['report']) && $_POST['report']) {
$period = $_POST['report'];
switch ($period) {
case 'today':
case 'yesterday':
case 'thisweek':
case 'lastweek':
case 'thismonth':
case 'lastmonth':
case 'thisyear':
case 'lastyear':
$start = date('Y-m-d', strtotime($periods[$period]['start']));
$end = date('Y-m-d', strtotime($periods[$period]['end']));
break;
case 'custom':
if (isset($_POST['start']) && $_POST['start'] &&
isset($_POST['end']) && $_POST['end']) {
$dt_start = DateTime::createFromFormat('d-m-Y', $_POST['start']);
$dt_end = DateTime::createFromFormat('d-m-Y', $_POST['end']);
if ($dt_start > $dt_end) {
echo 'Start date should be less than end date.';
exit;
}
$start = $dt_start->format('Y-m-d');
$end = $dt_end->format('Y-m-d');
} else {
echo 'Enter dates for report period.';
exit;
}
break;
default:
echo 'Invalid report period.';
exit;
}
} else {
echo 'Report period has not been selected';
exit;
}
第三 PDO部分保持完全相同,您只使用一次
// assumption is that $db is properly instantiated
$sql = "SELECT * FROM log WHERE log_datetime >= :start AND log_datetime < :end + INTERVAL 1 DAY";
$query = $db->prepare($sql);
$query->execute(array(':start'=>$start, ':end'=>$end));
$rows = $query->fetchAll(PDO::FETCH_OBJ);
您还可以在SQL中构建条件
-- today
SELECT *
FROM log
WHERE log_datetime >= CURDATE()
AND log_datetime < CURDATE() + INTERVAL 1 DAY;
-- yesterday
SELECT *
FROM log
WHERE log_datetime >= CURDATE() - INTERVAL 1 DAY
AND log_datetime < CURDATE();
-- this week
SELECT *
FROM log
WHERE log_datetime >= CURDATE() + INTERVAL 1 - DAYOFWEEK(CURDATE()) DAY
AND log_datetime < CURDATE() + INTERVAL 7 - DAYOFWEEK(CURDATE()) + 1 DAY;
-- this month
SELECT *
FROM log
WHERE log_datetime >= LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND log_datetime < LAST_DAY(CURDATE()) + INTERVAL 1 DAY;
-- this year
SELECT *
FROM log
WHERE log_datetime >= CURDATE() - INTERVAL DAYOFYEAR(CURDATE()) - 1 DAY
AND log_datetime < CURDATE() - INTERVAL DAYOFYEAR(CURDATE()) - 1 DAY + INTERVAL 1 YEAR;
-- last year
SELECT *
FROM log
WHERE log_datetime >= CURDATE() - INTERVAL DAYOFYEAR(CURDATE()) - 1 DAY - INTERVAL 1 YEAR
AND log_datetime < CURDATE() - INTERVAL DAYOFYEAR(CURDATE()) - 1 DAY;
这是 SQL Fiddle 演示
您甚至可以使用输入参数将其包装在存储过程中,该输入参数指示获取数据所需的时间段。
现在您不想做的事情是将任何函数(例如YEAR(log_datetime)
)应用于WHERE
子句中的列。通过这样做,您阻止 MySQL在该列上使用索引(索引)并有效地导致完整扫描。
答案 1 :(得分:0)
改变你的条件:
if(isset($_POST['option']) && empty($_POST['start']) && empty($_POST['end'])) {
echo 'Please don\'t try to hack!<br /><a href="./" title="Retry">Click here to <b>retry</b> again</a>';
exit;
}
最后与Select Report Time
而不是""
elseif ($option == "Select Report Time") {
$start_date = $_POST['start'];
$end_date = $_POST['end'];
$start = DateTime::createFromFormat('d-m-Y', $start_date);
$end = DateTime::createFromFormat('d-m-Y', $end_date);
$query = $db->prepare("SELECT * FROM log WHERE log_datetime >= :start AND log_datetime < :end + INTERVAL 1 DAY;");
$success = $query->execute(array(':start'=>$start->format('Y-m-d'), ':end'=>$end->format('Y-m-d')));
}