当我输入日期并单击搜索按钮时,我想获得两个日期之间的结果。到目前为止,我只进行了一次日期搜索。这是它的显示方式。
这是我的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/