gpt4 book ai didi

php - if elseif 语句导致输入字段强制使用选项值

转载 作者:行者123 更新时间:2023-11-29 00:21:23 31 4
gpt4 key购买 nike

首先,当我提交时,我的选择选项有效,但我的输入字段无效。我正在使用 if else if 语句根据选项查询我的数据库。

我怀疑这部分 elseif 不起作用:

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 选项,这是第一个 if 语句

这是我的表单代码:

<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>

这是我所有的 result.php php 代码,不包括 foreach :

if(empty($_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;
}
$option = $_POST['report'];

if($option == "Today") {
$query = $db->prepare("SELECT * FROM log WHERE log_datetime >= CURDATE();");
$success = $query->execute();
$startreportdate = date('d - M - Y');
$endreportdate = date('d - M - Y');
} elseif ($option == "Yesterday") {
$query = $db->prepare("SELECT * FROM log WHERE log_datetime >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND log_datetime < CURDATE();");
$success = $query->execute();
$startreportdate = date('d - M - Y', strtotime("-1 days"));
$endreportdate = date('d - M - Y', strtotime("-1 days"));
} elseif ($option == "This Week") {
$query = $db->prepare("SELECT * FROM log WHERE log_datetime >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) + 0 DAY);");
$success = $query->execute();
$startreportdate = date('d - M - Y');
$endreportdate = date('d - M - Y', strtotime("+6 days"));
} elseif ($option == "This Month") {
$query = $db->prepare("SELECT * FROM log WHERE log_datetime >= DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE()) - 1 DAY);");
$success = $query->execute();
$startreportdate = date('d - M - Y', strtotime("first day of this month"));
$endreportdate = date('d - M - Y', strtotime("last day of this month"));
} elseif ($option == "This Year") {
$query = $db->prepare("SELECT * FROM log WHERE YEAR(log_datetime) = YEAR(CURDATE());");
$startreportdate = date('d - M - Y', strtotime("first day of this month"));
$endreportdate = date('d - M - Y', strtotime("last day of this year"));
$success = $query->execute();
} elseif ($option == "Last Week") {
$query = $db->prepare("SELECT * FROM log WHERE log_datetime >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK) AND log_datetime < CURDATE();");
$success = $query->execute();
$startreportdate = date('d - M - Y', strtotime("-7 days"));
$endreportdate = date('d - M - Y', strtotime("-1 days"));
} elseif ($option == "Last Month") {
$query = $db->prepare("SELECT * FROM log WHERE YEAR(log_datetime) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(log_datetime) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH);");
$success = $query->execute();
$startreportdate = date('d - M - Y', strtotime("first day of last month"));
$endreportdate = date('d - M - Y', strtotime("last day of last month"));
} elseif ($option == "Last Year") {
$query = $db->prepare("SELECT * FROM log WHERE YEAR(log_datetime) = YEAR(CURDATE() - INTERVAL 1 YEAR);");
$success = $query->execute();
$startreportdate = date('d - M - Y', strtotime("first day of last year"));
$endreportdate = date('d - M - Y', strtotime("last day of last year"));
} 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";
}

$rows = $query->fetchAll(PDO::FETCH_OBJ);

我可以知道我哪里出错了吗?对不起,我的代码很乱,我还在学习,我正在使用 SO 和 php 手册作为引用,但是对于这个问题,我搜索了谷歌和 SO 高低但我找不到可以帮助我的确切问题和我必须问这个问题。

感谢所有提供帮助并花时间阅读此问题的人。谢谢。

最佳答案

您的直接问题是您的请求中没有 $_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>

更新 2:其次您可以检查请求参数并按照以下一致的方式生成开始和日期。

$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 在该列上使用索引(索引)并有效地导致完整扫描

关于php - if elseif 语句导致输入字段强制使用选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20936973/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com