Lisa234
Lisa234

Reputation: 69

searching 2 input fields PHP MySQL

this is my index page where the search fields are in.

index.php

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

this is my search.php page

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

                ?>

I want to add another search field where I can search both Date and Location in one search button and get the result where both location ad date is satisfied.

Upvotes: 0

Views: 433

Answers (2)

RJParikh
RJParikh

Reputation: 4166

Just add Input type for location and use it with its post variable.

When you click form submit button it will gives you all input data in post at server side.

Also change the name of post to 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());

Upvotes: 1

Exprator
Exprator

Reputation: 27513

add another input

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

in php

$Location=$_POST['Location'];

and in query

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

Upvotes: 1

Related Questions