在php中过滤数据表

时间:2016-08-08 06:12:04

标签: php

我有一个PHP网络应用程序,可以在日期范围之间过滤数据,但是当我从两个日期时间选择器中选择日期时,它没有显示,下面是脚本,请告诉我我缺少什么?

顺便说一句' $ post_at"和' $ post_at_to_date'是2日期时间选择器的变量:

$ post_at =" FROM Date"

$ post_at_to_date =" To Date"

  <?php

  if(isset($_POST['search']))
  {
    $post_at = $_POST['post_at'];
    $post_at_to_date =  $_POST['post_at_to_date'];
    $cmbDept = $_POST["search"];
    $query ="SELECT * FROM daily_data2 WHERE Checkdate BETWEEN '$post_at' and '$post_at_to_date' ";
    $search_result = filter($query);
   }
   else {
    $query = "SELECT * FROM daily_data2";
    $search_result = filter($query);
   }
   function filter($query)
   {
    $connect = mysqli_connect("localhost", "root", "","bio_db");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
   }                
 ?>

这是我的行动代码:

  <form name="frmSearch" method="post" action="index.php">
 <p class="search_input">
    <input type="text" placeholder="From Date" id="post_at" name="post_at"  value="<?php echo $post_at; ?>" class="input-control" />
    <input type="text" placeholder="To Date" id="post_at_to_date" name="post_at_to_date" style="margin-left:10px"  value="<?php echo $post_at_to_date; ?>" class="input-control"  />             
    <input type="submit" name="go" value="Search" >
</p>

对于填充表

  <table align="center" width="600" border="1" cellpadding="1" cellspacing="1">
        <tr>
            <th>Userid</th>
            <th>Name</th>
            <th>Campaign</th>
            <th>Checkdate</th>
            <th>Hoursworked</th>
            <th>Overtime</th>
        </tr>

    <?php while($row = mysqli_fetch_array($search_result)):?>
        <tr>
            <td style="text-align:center;"><?php echo $row['Userid'];?></td>
            <td width="200"><?php echo $row['Name'];?></td>
            <td style="text-align:center;"><?php echo $row['Campaign'];?></td>
            <td width="100" style="text-align:center;"><?php echo $row['Checkdate'];?></td>
            <td style="text-align:center;"><?php echo $row['Hoursworked'];?></td>
            <td style="text-align:center;"><?php echo $row['Overtime'];?></td>
        </tr>

    <?php endwhile;?>

        </table>

用于显示和过滤我的数据的mysql的SCRIPT。

       SELECT * FROM `daily_data2` WHERE Checkdate BETWEEN '2016-03-01' and '2016-03-31'

1 个答案:

答案 0 :(得分:1)

缺少

$post_at = $_POST["search"]['post_at'];
$post_at_to_date = $_POST["search"]['post_at_to_date'];

完整版

<?php

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

  if(isset($_POST['search'])
 &&  isset($_POST['search']['post_at'])
 &&  isset($_POST['search']['post_at_to_date'])
 ) {
 $post_at = $_POST["search"]['post_at'];
 $post_at_to_date = $_POST["search"]['post_at_to_date'];

    $query ="SELECT * FROM daily_data2 WHERE Checkdate BETWEEN '".$post_at."' and '".$post_at_to_date."' ";
    $search_result = filter($query);
   }
   else {
    $query = "SELECT * FROM daily_data2";
    $search_result = filter($query);
   }
   function filter($query)
   {
    $connect = mysqli_connect("localhost", "root", "","bio_db");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
   }