MaheshDigarse
MaheshDigarse

Reputation: 17

How retrieve specific data from database mysql

I have a search.php in this I'm searching for three fields:

But when I select a From no., from select option and enter in input a Telephone no. and press the search button it search for telephone no. instead of form no. I want a search like when I select From no. and enter a value of than it should search only for form no nothing else and like all below two option:

serach.php

<div class="tleft">
  <h2 class="tdilouge">Search Reports</h2><center>
  <table class="tbleft">
    <form action="searchbyform.php" method="get">
      <thead>
        <tr>
          <td>
             <select name="formnumber" id="formnumber" >
               <option>Form No.</option>                 
               <option>Name</option>             
               <option>Telephone No.</option>             

             </select>
          </td>
         <td><input type="text" name="formnumber" id="formnumber" placeholder="Enter" /></td>
          <td><input type="submit" value="Search" /></td>
        </tr>
      </thead>
    </form>
  </table></center>
</div>

And this is submit.php I have a proper connection of database and column is table name:

submit.php

<?php

   $formnumber = $_GET['formnumber'];
   $sql = "SELECT * FROM colomn WHERE FormNo = $formnumber OR ConsumerName = $formnumber OR TelephoneNo = $formnumber";
   $query = mysql_query( $sql );
          if(mysql_num_rows($query) == 0)
          {
          echo "no result found";
          }
          echo "<table>";

          echo "<thead></thead>";
                while( $row = mysql_fetch_array( $query ) )
                     {
                      echo "<tr></tr>";
                     }
          echo "</table>";
?>

Upvotes: 0

Views: 6818

Answers (1)

Loko
Loko

Reputation: 6679

You could just check which option the person has selected and depending on that selected option you could run the query that belong to that option. Give the options a value, like this:

(You should change the select name because the textfield is already named formnumber)

          <select name="form" id="form" >
           <option value="form">Form No.</option>                 
           <option value="name">Name</option>             
           <option value="telephone">Telephone No.</option>             

         </select>

So when you choose the option form no. , $_GET['form'] would be "form"

So just use an IF to check the 3 options.

EDIT: The query when the Form no. has been chosen, will look like this:

"SELECT * FROM colomn WHERE FormNo = $formnumber"

And for the name and telephone no. You should just change the column name.

if($_get['form']="form"){
$sql="SELECT * FROM colomn WHERE FormNo = $formnumber";
}

if($_get['form']="name"){
$sql="SELECT * FROM colomn WHERE ConsumerName = $formnumber";
}
if($_get['form']="telephone"){
$sql="SELECT * FROM colomn WHERE TelephoneNo = $formnumber";
}

Also dont use mysql. Use mysqli or PDO instead.

Upvotes: 1

Related Questions