在手动输入的两个日期之间搜索

时间:2017-06-30 10:49:04

标签: php mysql

我想在输入日期并点击搜索按钮时获得两个日期之间的结果。到目前为止我只进行了一次日期搜索。这是如何显示的。 enter image description here

这是我的 index.php

      <form  method="post" action="search.php?go"  id="searchform"> 
        <input  type="text" name="Date" placeholder="Date"> 
        <input  type="text" name="Location" placeholder="Location">
        <input  type="submit" name="submit" value="Search"> 
      </form>

这是我获得搜索结果的地方。 search.php

<?php

/* showing table after searching for date */

if (isset($_POST['submit'])) {
    if (isset($_GET['go'])) {
        $Date = $_POST['Date'];
        $Location = $_POST['Location'];

        $query = mysql_query("SELECT ID,Name,Location,Date,Category,LabourSupplier,InTime,OutTime,Day,DayRate,Salary,OTHours,OTrate,OTAmount,Allowance2,TotalSalary,Advance,SalaryToHand FROM attendance WHERE Date LIKE '%" . $Date . "%' AND Location LIKE '%" . $Location . "%' ORDER BY location DESC, LabourSupplier ASC", $connection)
        or die("Failed to query database" . mysql_error());

        while ($row = mysql_fetch_array($query)) {

            print "<tr>";
            print "<td >" . $row['ID'] . "</td>";
            print "<td >" . $row['Name'] . "</td>";
            print "<td >" . $row['Location'] . "</td>";
            print "<th >" . $row['Date'] . "</th>";
            print "<td >" . $row['Category'] . "</td>";
            print "<td >" . $row['LabourSupplier'] . "</td>";
            print "<th >" . $row['InTime'] . "</th>";
            print "<th >" . $row['OutTime'] . "</th>";
            print "<th >" . $row['Day'] . "</th>";
            print "<th >" . $row['DayRate'] . "</th>";
            print "<th >" . $row['Salary'] . "</th>";
            print "<th >" . $row['OTHours'] . "</th>";
            print "<th >" . $row['OTrate'] . "</th>";
            print "<th >" . $row['OTAmount'] . "</th>";
            print "<th >" . $row['Allowance2'] . "</th>";
            print "<th >" . $row['TotalSalary'] . "</th>";
            print "<th >" . $row['Advance'] . "</th>";
            print "<th>" . $row['SalaryToHand'] . "</th>";
            print "</tr>";
        }
    }

}
print "</table>";

?>

4 个答案:

答案 0 :(得分:1)

请仔细阅读此答案的结尾

添加另一个日期输入:

<form  method="post" action="search.php?go"  id="searchform"> 
  <input  type="date" name="Date1" placeholder="Date 1"> 
  <input  type="date" name="Date2" placeholder="Date 2"> 
  <input  type="text" name="Location" placeholder="Location">
  <input  type="submit" name="submit" value="Search"> 
</form>

在PHP脚本中添加新的日期输入值:

<?php

/* showing table after searching for date */

if (isset($_POST['submit'])) {
    if (isset($_GET['go'])) {
        $Date1 = $_POST['Date1'];
        $Date2 = $_POST['Date2'];
        $Location = $_POST['Location'];

        $query = mysqli_query($connection, "SELECT ID,Name,Location,Date,Category,LabourSupplier,InTime,OutTime,Day,DayRate,Salary,OTHours,OTrate,OTAmount,Allowance2,TotalSalary,Advance,SalaryToHand FROM attendance WHERE Date BETWEEN $Date1 AND $Date2 AND Location LIKE '%" . $Location . "%' ORDER BY location DESC, LabourSupplier ASC")
        or die("Failed to query database" . mysql_error());

        while ($row = mysqli_fetch_array($query)) {

            print "<tr>";
            print "<td >" . $row['ID'] . "</td>";
            print "<td >" . $row['Name'] . "</td>";
            print "<td >" . $row['Location'] . "</td>";
            print "<th >" . $row['Date'] . "</th>";
            print "<td >" . $row['Category'] . "</td>";
            print "<td >" . $row['LabourSupplier'] . "</td>";
            print "<th >" . $row['InTime'] . "</th>";
            print "<th >" . $row['OutTime'] . "</th>";
            print "<th >" . $row['Day'] . "</th>";
            print "<th >" . $row['DayRate'] . "</th>";
            print "<th >" . $row['Salary'] . "</th>";
            print "<th >" . $row['OTHours'] . "</th>";
            print "<th >" . $row['OTrate'] . "</th>";
            print "<th >" . $row['OTAmount'] . "</th>";
            print "<th >" . $row['Allowance2'] . "</th>";
            print "<th >" . $row['TotalSalary'] . "</th>";
            print "<th >" . $row['Advance'] . "</th>";
            print "<th>" . $row['SalaryToHand'] . "</th>";
            print "</tr>";
        }
    }

}
print "</table>";

?>

<强> INPORTANT

请尝试使用PHP-PDO而不是常规旧的弃用的MySQL函数。您的脚本容易受到SQL注入攻击,强烈建议您更改它

答案 1 :(得分:0)

使用两个日期,而不是单个日期<input type="text" name="Date" placeholder="Date">

<input  type="text" name="from_date" placeholder="Date">
<input  type="text" name="to_date" placeholder="Date">

并在查询中使用它,如:

WHERE Date BETWEEN $from_date and $to_date

注意:但是你必须记住sql注入问题。

答案 2 :(得分:0)

SELECT * 出席 在什么日期之间(&#39; 2014-02-01&#39; AS DATE)和CAST(&#39; 2014-02-28&#39; AS DATE)之间的日期;

答案 3 :(得分:0)

好吧,我得到了我想要的东西。我只需要改变&#34; index.php&#34;

        <input  type="text" name="FDate" placeholder="From Date"> 
        <input  type="text" name="TDate" placeholder="To Date">

和&#34; search.php&#34;

$query = mysql_query("SELECT ID,Name,Location,Date,Category,...,SalaryToHand FROM attendance WHERE Date BETWEEN '".$_POST["FDate"]."' AND '".$_POST["TDate"]."' AND Location LIKE '%" . $Location . "%' ORDER BY location DESC, Date DESC",$connection)
相关问题