我正在尝试执行 MySQLI 准备好的语句,该语句将查询 MySQL 数据库并根据表单中请求的数据返回结果。我已经能够让这个准备好的语句正常工作,它可以正确地恢复数据。现在我希望能够在查询中添加一个“日期范围”,但我无法让它工作。From
我通过创建和To
字段在请求表单的顶部添加了一个日期选择器input type="date"
。MM/DD/YYYY
这会将日期添加到格式的字段中。在数据库中,我正在检查的列是一种date
类型,格式为YYYY-MM-DD
. 我已经尝试了这个网站的几个不同的建议,以及我在搜索中遇到的其他建议,但得到了错误。我试过使用strtotime
和date
并得到日期格式错误和致命错误的错误can't construct DatePeriod()
。我正在使用 PHP v5.5,所以我读到你可以DateTime::createFromFormat
一起使用$string->format
,然后使用DatePeriod
,这会起作用,但我得到另一个致命错误Call to member function format() on a non-object
。请帮忙。
这是“搜索表单”的代码
<form action="../includes/test.inc.php" method="get">
<table border="0" cellspacing="1">
<tr>
Dates:<br>
From: <input type="date" id="from" name="from">
To: <input type="date" id="to" name="to">
</br></br>
Result: <input type="text" name="result" id="result" /><br>
Employee: <input type="text" name="employee" id="employee" /><br>
Project: <input type="text" name="project" id="project" /><br>
Source: <input type="text" name="source" id="source" /><br>
Appointment Date: <input type="text" name="appt_date" id="appt_date" /><br>
Branch: <input type="text" name="branch" id="branch" /><br>
First Name: <input type="text" name="fname" id="fname" /><br>
Last Name: <input type="text" name="lname" id="lname" /><br>
Last Four: <input type="text" name="last_four" id="last_four" /><br>
Phone: <input type="text" name="phone" id="phone" /><br>
City: <input type="text" name="city" id="city" /><br>
State: <input type="text" name="state" id="state" /><br>
Zip: <input type="text" name="zip" id="zip" /><br>
<input type="submit" value="submit" />
</tr>
</table>
</form>
这是“表单处理操作”的代码
<?php
include_once 'db_connect.php';
include_once 'psl-config.php';
session_start();
$error_msg = "";
if (isset($_GET['from']))
$date = $_GET['from'];
if (isset($_GET['to']))
$date2 = $_GET['to'];
if (isset($_GET['set_date']))
$set_date = $_GET['set_date'];
if (isset($_GET['result']))
$result = $_GET['result'];
if (isset($_GET['employee']))
$employee = $_GET['employee'];
if (isset($_GET['project']))
$employee = $_GET['project'];
if (isset($_GET['source']))
$source = $_GET['source'];
if (isset($_GET['appt_date']))
$appt_date = $_GET['appt_date'];
if (isset($_GET['branch']))
$branch = $_GET['branch'];
if (isset($_GET['fname']))
$fname = $_GET['fname'];
if (isset($_GET['lname']))
$lname = $_GET['lname'];
if (isset($_GET['last_four']))
$last_four = $_GET['last_four'];
if (isset($_GET['phone']))
$phone = $_GET['phone'];
if (isset($_GET['city']))
$city = $_GET['city'];
if (isset($_GET['state']))
$state = $_GET['state'];
if (isset($_GET['zip']))
$zip = $_GET['zip'];
$query = $mysqli->prepare("SELECT set_date, result, employee, project, source, appt_date, branch, fname, lname, last_four, phone, city, state, zip, monthly_net, job_time FROM appointments WHERE set_date LIKE CONCAT('%', ?, '%') AND result LIKE CONCAT('%', ?, '%') AND employee LIKE CONCAT('%', ?, '%') AND project LIKE CONCAT('%', ?, '%') AND source LIKE CONCAT('%', ?, '%') AND appt_date LIKE CONCAT('%', ?, '%') AND branch LIKE CONCAT('%', ?, '%') AND fname LIKE CONCAT('%', ?, '%') AND lname LIKE CONCAT('%', ?, '%') AND last_four LIKE CONCAT('%', ?, '%') AND phone LIKE CONCAT('%', ?, '%') AND city LIKE CONCAT('%', ?, '%') AND state LIKE CONCAT('%', ?, '%') AND zip LIKE CONCAT('%', ?, '%') ORDER BY employee");
if (isset($_GET['from'])) {
$date = DateTime::createFromFormat('m/d/Y', $_GET['from']);
$date2 = DateTime::createFromFormat('m/d/Y', $_GET['to']);
$from = $date->format('Y-m-d');
$to = $date2->format('Y-m-d');
$daterange = new DatePeriod($from, $to);
foreach($daterange as $dr) {
$query->bind_param('sssssssssssssss', $dr, $_GET['set_date'], $_GET['result'], $_GET['employee'], $_GET['project'], $_GET['source'], $_GET['appt_date'], $_GET['branch'], $_GET['fname'], $_GET['lname'], $_GET['last_four'], $_GET['phone'], $_GET['city'], $_GET['state'], $_GET['zip']);
$query->execute();
}
}
$query->store_result();
$query->bind_result($set_date, $result, $employee, $project, $source, $appt_date, $branch, $fname, $lname, $last_four, $phone, $city, $state, $zip);
$rows = $query->num_rows;
$results = array();
while($row = $query->fetch()) {
$results[] = array(
'rows' => $rows,
'set_date' => $set_date,
'result' => $result,
'employee' => $employee,
'project' => $project,
'source' => $source,
'appt_date' => $appt_date,
'branch' => $branch,
'fname' => $fname,
'lname' => $lname,
'last_four' => $last_four,
'phone' => $phone,
'city' => $city,
'state' => $state,
'zip' => $zip
);
}
$_SESSION['results'] = $results;
if($results) {
header('Location: ../test_page.php');
}else{
header('Location: ../test.php?error=1');
}
$query->free_result();
$mysqli->close();
?>
更新:我已经能够消除我收到的所有错误,但仍然无法使日期范围正常工作。我没有更改“表单页面”上的任何代码,但这里是“处理页面”的更新代码。现在我被发送到“error=1”页面,好像没有结果一样,但我知道有结果,如果我删除日期范围部分,它们会正确返回。
<?php
include_once 'db_connect.php';
include_once 'psl-config.php';
session_start();
$error_msg = "";
if (isset($_GET['from']))
$date = $_GET['from'];
if (isset($_GET['to']))
$date2 = $_GET['to'];
if (isset($_GET['set_date']))
$set_date = $_GET['set_date'];
if (isset($_GET['result']))
$result = $_GET['result'];
if (isset($_GET['employee']))
$employee = $_GET['employee'];
if (isset($_GET['project']))
$employee = $_GET['project'];
if (isset($_GET['source']))
$source = $_GET['source'];
if (isset($_GET['appt_date']))
$appt_date = $_GET['appt_date'];
if (isset($_GET['branch']))
$branch = $_GET['branch'];
if (isset($_GET['fname']))
$fname = $_GET['fname'];
if (isset($_GET['lname']))
$lname = $_GET['lname'];
if (isset($_GET['last_four']))
$last_four = $_GET['last_four'];
if (isset($_GET['phone']))
$phone = $_GET['phone'];
if (isset($_GET['city']))
$city = $_GET['city'];
if (isset($_GET['state']))
$state = $_GET['state'];
if (isset($_GET['zip']))
$zip = $_GET['zip'];
if (isset($_GET['from'])) {
$from = new DateTime($_GET['from']);
$to = new DateTime($_GET['to']);
var_dump($from, $to);
$interval = DateInterval::createFromDateString('1 day');
$daterange = new DatePeriod($from, $interval, $to);
if (isset($daterange)) {
$query = $mysqli->prepare("SELECT set_date, result, employee, project, source, appt_date, branch, fname, lname, last_four, phone, city, state, zip FROM appointments WHERE set_date LIKE CONCAT('%', ?, '%') AND result LIKE CONCAT('%', ?, '%') AND employee LIKE CONCAT('%', ?, '%') AND project LIKE CONCAT('%', ?, '%') AND source LIKE CONCAT('%', ?, '%') AND appt_date LIKE CONCAT('%', ?, '%') AND branch LIKE CONCAT('%', ?, '%') AND fname LIKE CONCAT('%', ?, '%') AND lname LIKE CONCAT('%', ?, '%') AND last_four LIKE CONCAT('%', ?, '%') AND phone LIKE CONCAT('%', ?, '%') AND city LIKE CONCAT('%', ?, '%') AND state LIKE CONCAT('%', ?, '%') AND zip LIKE CONCAT('%', ?, '%') ORDER BY employee");
$query->bind_param('ssssssssssssss', $_GET['set_date'], $_GET['result'], $_GET['employee'], $_GET['project'], $_GET['source'], $_GET['appt_date'], $_GET['branch'], $_GET['fname'], $_GET['lname'], $_GET['last_four'], $_GET['phone'], $_GET['city'], $_GET['state'], $_GET['zip']);
$query->execute();
}
}
$query->store_result();
$query->bind_result($set_date, $result, $employee, $project, $source, $appt_date, $branch, $fname, $lname, $last_four, $phone, $city, $state, $zip);
$rows = $query->num_rows;
$results = array();
while($row = $query->fetch()) {
$results[] = array(
'rows' => $rows,
'set_date' => $set_date,
'result' => $result,
'employee' => $employee,
'project' => $project,
'source' => $source,
'appt_date' => $appt_date,
'branch' => $branch,
'fname' => $fname,
'lname' => $lname,
'last_four' => $last_four,
'phone' => $phone,
'city' => $city,
'state' => $state,
'zip' => $zip
);
}
$_SESSION['results'] = $results;
if($results) {
header('Location: ../test_page.php');
}else{
header('Location: ../test.php?error=1');
}
$query->free_result();
$mysqli->close();
?>