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

标签 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>";

?>

最佳答案

请阅读本回答的结尾

添加另一个日期输入:

<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>";

?>

重要

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

关于php - 在手动输入的两个日期之间搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44844686/

相关文章:

javascript - JS点击两次后不显示文字

php - 使用 jquery 文件上传后输入文件字段为空

php - 如何根据标志字段 0/1 显示 "Yes/No"CGridView yii?

mysql - mysql中的TABLE_LIST结构在哪里?

Mysql - 从一个表中选择一个随机行并将其连接到另一个表中的三行

php - Laravel 无法更新用户

php - 选择并计算连接表

php - 与 MySQLi 绑定(bind)参数抛出 "undefined method"错误?

mysql - 如何在 MySQL 中显示表的唯一约束?

PHP + MySQLi - 尝试使用 enctype ="multipart/form-data"将图像上传到本地主机数据库中的用户行