-1

MySQLデータベースにクエリを実行し、フォームで要求されたデータに応じて結果を返すMySQLI準備済みステートメントを実行しようとしています。この準備済みステートメントを機能させることができ、データが正しく返されました。今、クエリに「日付範囲」を追加できるようにしたいのですが、これを機能させることができません。FromToフィールドを作成して、リクエスト フォームの上部に日付ピッカーを追加しましたinput type="date"。これにより、フォーマットのフィールドに日付が追加されMM/DD/YYYYます。データベースでは、チェックしている列はdateタイプであり、形式は ですYYYY-MM-DD。このサイトや検索で出くわした他のサイトからいくつかの異なる提案を試みましたが、エラーが発生しました。私は使用しようとしましstrtotimedate日付の形式が間違っているというエラーと致命的なエラーが発生します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:&nbsp;<input type="date" id="from" name="from"> 

&nbsp;&nbsp;&nbsp;

            To:&nbsp;<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();
?>
4

2 に答える 2

0

私はついにこれを理解することができました。必要以上に複雑にしようとしていたようです。フォームから入力された日付範囲でクエリを動作させるために使用したコードは次のとおりです

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 ステートメントの and$from = date("Y-m-d", strtotime($from)部分だけが必要でした。すべての提案に感謝し、この回答が他の人に役立つことを願っています$to = date("Y-m-d", strtotime($to)WHERE set_date BETWEEN '".$from."' AND '".$to."'

于 2015-07-17T20:40:49.300 に答える
-1
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

于 2015-07-14T17:34:15.220 に答える