2017-06-30 96 views
1

我想在輸入日期並單擊搜索按鈕時在兩個日期之間得到結果。目前爲止,我只能進行一次日期搜索。這是它如何顯示。 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>"; 

?> 
+1

不要使用mysql_ *功能annymore使用mysqli_ *功能或PDO。 mysql_ *函數已被棄用 –

+1

您的SQL查詢很容易出現SQL注入 –

+0

忘記所有這些PHP的東西;這與你的問題無關,而且非常非常過時。相反,請參閱:[爲什麼我應該爲我認爲是非常簡單的SQL查詢提供一個MCVE?](https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an- mcve-for-what-looks-to-what-seems-to-very-simple-sql-query) – Strawberry

回答

0

好吧我得到了我想要的東西。我不得不改變只有 「的index.php」

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

和 「的search.php」

$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) 
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注入問題。

+1

那麼這可能會回答主題首發問題......這個建議很容易出現SQL注入。 –

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

這可能會回答topicstarter問題..答案不應該包括不推薦使用的函數,如mysql_ * –

+0

編輯@RaymondNijland感謝您的notif – droidnation

+0

它是mysqli_query($連接,$查詢)不是mysqli_query($查詢) –

0

SELECT * FROM 出席 WHERE日之間的CAST( '2014年2月1日' AS DATE)和CAST('2014年2月28日'AS DATE);

+1

您不需要CAST日期..這也適用於'SELECT * FROM attendance WHERE日期'2014-02-01'和'2014-02-28'' –

相關問題