过滤结果基于From&至今

时间:2016-11-21 09:45:58

标签: javascript php html

我正在尝试使用以下代码来过滤基于From&的值。到了日期,但是当我点击搜索按钮时,它只是刷新页面并显示相同的页面。 但不显示过滤结果....

enter image description here

PHP

$reg_user='';

$post_at = "";
$post_at_to_date = "";

    $queryCondition = "";
    if(!empty($_POST["search"]["post_at"])) 
{           
    $post_at = date('Y-m-d 0:0:0',strtotime($_POST["search"]["post_at"]));
    $post_at_todate = date('Y-m-d:59:59:59');
    if(!empty($_POST["search"]["post_at_to_date"])) {
        $post_at_to_date = date('Y-m-d 59:59:59',strtotime($_POST["search"]["post_at_to_date"]));
        $post_at_todate = $post_at_to_date;
    }

$queryCondition .= "WHERE post_at >= '" . $post_at . "' AND post_at <= '" . $post_at_todate . "'";

$reg_user = new USER();
$stmt = $reg_user->runQuery("SELECT * FROM order_details ".$queryCondition);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->execute();

HTML

<form name="frmSearch" method="post" action="">

<input type="text" placeholder="From Date" id="post_at"  
value="<?php echo $post_at; ?>" name="search[post_at]" />       
<input type="text" placeholder="To Date" id="post_at_to_date" value="<?php echo $post_at_to_date; ?>" 
name="search[post_at_to_date]" onkeyup="doFilter()" />
<input type="submit" name="search" value="search" >

脚本

<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src= "http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.js"></script>
<script>
$.datepicker.setDefaults({
showOn: "button",
buttonImage: "datepicker.png",
buttonText: "Date Picker",
buttonImageOnly: true,
dateFormat: 'yy-mm-dd'  
});
$(function() {
$("#post_at").datepicker();
$("#post_at_to_date").datepicker();
});
</script>

日期的列名称:created_at,表格名称 - order_details。我是php的新手,请帮助我。

修改

当我尝试var_dump($post_at); var_dump($post_at_to_date);时,它会给出结果:string(0) "" string(0) ""

编辑2 我正在尝试下面的代码,现在var_dump正在显示结果,但这些值不是基于所选的From&amp;到目前为止

echo "<pre>";print_r($_POST);

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$reg_user='';

$post_at = "";
$post_at_to_date = "";
$queryCondition = "";
    if(!empty($_POST["post_at"])) 
    {           
        $post_at = date('Y-m-d 0:0:0',strtotime($_POST["post_at"]));
        $post_at_todate = date('Y-m-d:59:59:59');
        if(!empty($_POST["post_at_to_date"])) {
            $post_at_to_date = date('Y-m-d 59:59:59',strtotime($_POST["post_at_to_date"]));
            $post_at_todate = $post_at_to_date;
        }
        $queryCondition .= "WHERE post_at >= '" . $post_at . "' AND post_at <= '" . $post_at_todate . "'";
    }

    $sqlquery = "SELECT * FROM order_details $queryCondition";
    $result = $conn->query($sqlquery);

    if ($result->num_rows > 0) {
        echo $result->num_rows.'result found'."<br>";
        while($row = $result->fetch_assoc()) {
            echo "id: " . $row["id"]. " - date: " . $row["post_at"];
            echo "<br>";
        }
    } else {
        echo "0 results found <br>";
    }
$conn->close();

形式

<form name="frmSearch" method="post" action="">
<input type="text" placeholder="From Date" id="post_at" value="" name="post_at" />       
<input type="text" placeholder="To Date" id="post_at_to_date" value="" name="post_at_to_date" />
<input type="submit" name="search" value="search" />
</form>

脚本

jQuery.datepicker.setDefaults({
showOn: "button",
buttonImage: "datepicker.png",
buttonText: "Date Picker",
buttonImageOnly: true,
dateFormat: 'yy-mm-dd'  
});
$(function() {
$("#post_at").datepicker();
$("#post_at_to_date").datepicker();
});

结果:

Array
(
    [post_at] => 
    [post_at_to_date] => 2016-11-17
    [search] => search
)

24result发现

注意:未定义的索引:echo "id: " . $row["id"]. " - date: " . $row["post_at"];中的id&amp;显示相同的页面。

id: - date:0000-00-00 00:00:00

2 个答案:

答案 0 :(得分:0)

尝试CAST:

$ queryCondition。=&#34;其中CAST(post_at为DATE)&gt; = CAST(&#34;。$ post_at。&#34;作为DATE) 和 CAST(post_at作为DATE)&lt; = CAST(&#34;。$ post_at_todate。&#34;作为DATE)&#34;

答案 1 :(得分:0)

假设您的数据库中的日期字段为date而不是datetime,那么这应该有效,您当前正在使用无效的日期时间。

if( !empty( $_POST[ 'post_at' ] ) ) 
{           
    $post_at        = date( 'Y-m-d', strtotime( $_POST[ 'post_at' ] ) );
    $post_at_todate = date( 'Y-m-d' );
    if( !empty( $_POST[ 'post_at_to_date' ] ) )
    {
        $post_at_to_date = date( 'Y-m-d', strtotime( $_POST[ 'post_at_to_date' ] ) );
        $post_at_todate = $post_at_to_date;
    }
    $queryCondition .= "WHERE DATE( created_at ) >= '" . $post_at . "' AND DATE( created_at ) <= '" . $post_at_todate . "'";
}

$sqlquery = "SELECT * FROM order_details $queryCondition";

某些日期格式错误,请查看var_dump( $sqlquery );并确保格式正确。然后确保结果通过。