MySQLI 准备语句并显示在表单上使用 PHP 日期选择器输入的日期范围的查询结果
MySQLI prepared statement and show results of query for a date range entered with a PHP Datepicker on Form
我正在尝试执行一个 MySQL我准备好的语句,该语句将查询 MySQL 数据库并 return 结果取决于表单中请求的数据。我已经能够让这个准备好的语句工作并且它正确地带回了数据。现在我希望能够向查询中添加一个 'date range',但我无法让它工作。我通过使 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
.请帮忙。
这是 'Search Form'
的代码
<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>
这是 'Form Processing Action'
的代码
<?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();
?>
更新:我已经能够消除我收到的所有错误,但仍然无法使日期范围正常工作。我没有更改 'Form Page' 上的任何代码,但这里是 'Processing Page' 的更新代码。现在我被发送到 '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();
?>
function new_date($str) {
$str = strtotime($str);
$str = date('d-m-Y',$str);
return $str;
}
用法:new_date('YYYY-MM-DD');
输出:DD-MM-YYYY
function new_date($str) {
$str = strtotime($str);
$str = date('Y-m-d',$str);
return $str;
}
用法:new_date('DD-MM-YYYY');
输出:YYYY-MM-DD
我终于弄明白了。似乎我试图使它变得比需要的更复杂。这是我用来获取查询以处理从表单
输入的日期范围的代码
include_once 'db_connect.php';
include_once 'psl-config.php';
session_start();
$error_msg = "";
if (isset($_GET['from']))
$from = $_GET['from'];
if (isset($_GET['to']))
$to = $_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'];
$from = date("Y-m-d", strtotime($from));
$to = date("Y-m-d", strtotime($to));
$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 BETWEEN '".$from."' AND '".$to."' AND 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 set_date ASC");
$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: ../appointments_page.php');
}else{
header('Location: ../appointments.php?error=1');
}
$query->free_result();
$mysqli->close();
?>
我的 SELECT 语句中只需要 $from = date("Y-m-d", strtotime($from)
和 $to = date("Y-m-d", strtotime($to)
以及 WHERE set_date BETWEEN '".$from."' AND '".$to."'
部分。感谢所有的建议,希望这个答案可以帮助到其他人
我正在尝试执行一个 MySQL我准备好的语句,该语句将查询 MySQL 数据库并 return 结果取决于表单中请求的数据。我已经能够让这个准备好的语句工作并且它正确地带回了数据。现在我希望能够向查询中添加一个 'date range',但我无法让它工作。我通过使 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
.请帮忙。
这是 'Search Form'
的代码<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>
这是 'Form Processing Action'
的代码<?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();
?>
更新:我已经能够消除我收到的所有错误,但仍然无法使日期范围正常工作。我没有更改 'Form Page' 上的任何代码,但这里是 'Processing Page' 的更新代码。现在我被发送到 '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();
?>
function new_date($str) {
$str = strtotime($str);
$str = date('d-m-Y',$str);
return $str;
}
用法:new_date('YYYY-MM-DD'); 输出:DD-MM-YYYY
function new_date($str) {
$str = strtotime($str);
$str = date('Y-m-d',$str);
return $str;
}
用法:new_date('DD-MM-YYYY'); 输出:YYYY-MM-DD
我终于弄明白了。似乎我试图使它变得比需要的更复杂。这是我用来获取查询以处理从表单
输入的日期范围的代码include_once 'db_connect.php';
include_once 'psl-config.php';
session_start();
$error_msg = "";
if (isset($_GET['from']))
$from = $_GET['from'];
if (isset($_GET['to']))
$to = $_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'];
$from = date("Y-m-d", strtotime($from));
$to = date("Y-m-d", strtotime($to));
$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 BETWEEN '".$from."' AND '".$to."' AND 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 set_date ASC");
$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: ../appointments_page.php');
}else{
header('Location: ../appointments.php?error=1');
}
$query->free_result();
$mysqli->close();
?>
我的 SELECT 语句中只需要 $from = date("Y-m-d", strtotime($from)
和 $to = date("Y-m-d", strtotime($to)
以及 WHERE set_date BETWEEN '".$from."' AND '".$to."'
部分。感谢所有的建议,希望这个答案可以帮助到其他人