搜索2个输入字段PHP MySQL

时间:2017-06-28 09:08:31

标签: php mysql

这是搜索字段所在的索引页面。

的index.php

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

这是我的search.php页面

<?php

/* showing table after searching for date */

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

    $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 . "%' 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>"; 

                ?>

我想添加另一个搜索字段,我可以在一个搜索按钮中搜索日期和位置,并获得满足两个位置广告日期的结果。

2 个答案:

答案 0 :(得分:1)

添加其他输入

    <input  type="text" name="Location"> 

在php中

$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)

答案 1 :(得分:1)

只需添加位置的输入类型,并将其与其post变量一起使用。

当您单击表单提交按钮时,它将为您提供服务器端发布的所有输入数据。

同时将帖子名称更改为submit1 $_POST['submit1']

<强>的index.php

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

<强>的search.php

<?php

/* showing table after searching for date */
if(isset($_POST['submit1'])){
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 Location = '".$Location."' Date LIKE '%" . $Date . "%' ORDER BY location DESC, LabourSupplier ASC",$connection)
        or die("Failed to query database" .mysql_error());