通过复选框从mysql中检索数据

I want to search question from mysql using ComboBox. If I select chapter number one in ComboBox, I want to display that question which only chapter one have.

In this suppose my chapter 1 contains 2 questions, chapter 2 contains some questions and so on. When I select chapter number 1 then it doesn't display question that chapter 1 have. It will only print the last question from the last chapter. How can I solve this problem?

 <?php      

      $sql= "select distinct chapter from math";        
      $q= mysql_query($sql);        
      echo "<select name='fname'>";     
      while($info=mysql_fetch_array($q)){
      $d1 = $info['chapter'];       
      echo "<option> ".$info['chapter']."</option>";          
      }
      echo "</select>";         
      $sql1 = "select question from math where chapter=$d1";        
      $sql1_res = mysql_query($sql1) or die(mysql_error());     
      while($row = mysql_fetch_array($sql1_res)){  
              $question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
      echo $question;           
      } 
  ?>

Your mistake is that you are selecting questions from last iteration of query where you select chapters.

...
while($info=mysql_fetch_array($q)){
  $d1 = $info['chapter'];       
  echo "<option> ".$info['chapter']."</option>";          
}
echo "</select>";         
$sql1 = "select question from math where chapter=$d1"; 
...

will always select last chapter. But you have another problem. Assuming you want to show questions when user select some value from dropdown, you have to send that selection using POST/GET/AJAX back to PHP and generate results based on that selection. Something like this:

    if(!isset($_POST['fname']))
    {
       $sql= "select distinct chapter from math";        
       $q= mysql_query($sql);        
       echo "<select name='fname'>";  

       while($info=mysql_fetch_array($q)){     
         echo "<option> ".$info['chapter']."</option>";          
       }
       echo "</select>";      
    }   
    else
    {
      $sql1 = "select question from math where chapter = " . $_POST['fname'];        
      $sql1_res = mysql_query($sql1) or die(mysql_error());     
      while($row = mysql_fetch_array($sql1_res)){  
        $question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
        echo $question;           
      } 
    }

In your question query change the while loop according to below code:

<?php      

      $sql= "select distinct chapter from math";        
      $q= mysql_query($sql);        
      echo "<select name='fname'>";     
      $d1 = array();
      while($info=mysql_fetch_array($q)){
         $d1[] = $info['chapter'];       
         echo "<option> ".$info['chapter']."</option>";          
      }
      echo "</select>";         
      $sql1 = "select question from math where chapter IN ('".implode("','",$d1)."')";        
      $sql1_res = mysql_query($sql1) or die(mysql_error());     
      while($row = mysql_fetch_array($sql1_res)){  
        $question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
        echo $question;           
      } 
  ?>

make the $d1 variable as an array and change the select query of question.

 <?php      

      $sql= "select distinct chapter from math";        
      $q= mysql_query($sql);        
      echo "<select name='fname'>";     
      while($info=mysql_fetch_array($q))
     {
        $d1 = $info['chapter'];       
        echo "<option> ".$info['chapter']."</option>";          

        echo "</select>";         
        $sql1 = "select question from math where chapter=$d1";        
        $sql1_res = mysql_query($sql1) or die(mysql_error());     
         while($row = mysql_fetch_array($sql1_res))
          {  
              $question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
      echo $question;           
         } 
    }
  ?>
$sql1 = "select question from math where chapter=".$d1;