MySQLデータベースにクエリを実行し、フォームで要求されたデータに応じて結果を返すMySQLI準備済みステートメントを実行しようとしています。この準備済みステートメントを機能させることができ、データが正しく返されました。今、クエリに「日付範囲」を追加できるようにしたいのですが、これを機能させることができません。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();
?>
更新: 受け取っていたすべてのエラーを取り除くことができましたが、まだ日付範囲を機能させることができません。「フォーム ページ」のコードは変更しませんでしたが、「処理ページ」の更新コードは次のとおりです。結果がないかのように「エラー = 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();
?>