In today’s tutorial, we will see how to bind dropdown box with the MySQL table data i.e. content of the Dropdown list will not be fixed rather it will come from MySQL table.

With the Dropdown list we have used config file in this tutorial.

Lets see that config.php file first :

<?php
 $databaseHost = "localhost"; 
 $databaseUser = "root";
 $databasePassword = "";
 $databaseName = "employee";
       
      $con=mysql_connect($databaseHost ,$databaseUser ,$databasePassword )or die ('Connection Error');
      mysql_select_db("employee",$con) or die ('Database Error');
 ?>

This config  file we will use in our php script to get connected and to select database.After that we will see how to bind dropdown list with MySQL using PHP. Here we are fetching unique data of designation filed form emp table.

 <strong> Select Designation : </strong> 
 <select name="empName"> 
        <option value=""> -----------ALL----------- </option> 
      <?php
          $dd_res=mysql_query("Select DISTINCT designation from emp");
          while($r=mysql_fetch_row($dd_res))
          { 
                echo "<option value='$r[0]'> $r[0] </option>";
          }
      ?>
 </select>

Output :

Dropdown list in PHP

After binding dropdown list, whenever user selects any designation, based on that record will be displayed in table.

 <?php
   if($_SERVER['REQUEST_METHOD'] == "POST")
   {
         $des=$_POST["empName"]; 
         if($des=="")
         {     
               $res=mysql_query("Select * from emp");   // if ALL is selected in dropdown
         }
         else
        {  
              $res=mysql_query("Select * from emp where designation='".$des."'");  // if any designation is selected
        }
        echo "<tr><td colspan='5'></td></tr>";
        while($r=mysql_fetch_row($res))
        {
            echo "<tr>";
            echo "<td align='center'>$r[0]</td>";
            echo "<td width='200'>$r[1]" . " $r[2]</td>";
            echo "<td align='center' width='40'> $r[3]</td>";
            echo "<td align='center' width='200'>$r[4]</td>";
            echo "<td width='100' align='center'>$r[5]</td>";
            echo "</tr>";
        }
    }
?>

Output :

Dropdown list in PHP1

Complete Code :

 <?php
      include "config.php";  // including configuration file
?>
 <html>
 <body>
     <form name="frmdropdown" method="post" action="emp_dd_display.php">
     <center>
            <h2 align="center">Employee Data</h2>
        
            <strong> Select Designation : </strong> 
            <select name="empName"> 
               <option value=""> -----------ALL----------- </option> 
            <?php
 
                 $dd_res=mysql_query("Select DISTINCT designation from emp");
                 while($r=mysql_fetch_row($dd_res))
                 { 
                       echo "<option value='$r[0]'> $r[0] </option>";
                 }
             ?>
              </select>
     <input type="submit" name="find" value="find"/> 
     <br><br>
 
   <table border="1">
 <tr align="center">
     <th>Emp_Id </th>      <th>Emp_name </th>     <th>Age</th>    <th>Designation</th>    <th>Salary</th>
 </tr> 

 <?php 
  if($_SERVER['REQUEST_METHOD'] == "POST")
  {
         $des=$_POST["empName"]; 
         if($des=="")  // if ALL is selected in Dropdown box
         { 
             $res=mysql_query("Select * from emp");
         }
         else
         { 
             $res=mysql_query("Select * from emp where designation='".$des."'");
         }
 
         echo "<tr><td colspan='5'></td></tr>";
         while($r=mysql_fetch_row($res))
         {
                 echo "<tr>";
                 echo "<td align='center'>$r[0]</td>";
                 echo "<td width='200'>$r[1]" . " $r[2]</td>";
                 echo "<td alig='center' width='40'> $r[3]</td>";
                 echo "<td align='center' width='200'>$r[4]</td>";
                 echo "<td width='100' align='center'>$r[5]</td>";
                 echo "</tr>";
        }
    }
?>
  </table>
 </center>
</form>
</body>
</html>