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

时间:2013-08-28 11:16:45

标签: php html mysql

我想使用ComboBox从mysql中搜索问题。如果我在ComboBox中选择第一章,我想显示只有第一章有的那个问题。

在这个假设中,我的第1章包含2个问题,第2章包含一些问题,等等。当我选择第1章时,它不会显示第1章的问题。它只会打印上一章中的最后一个问题。我怎么解决这个问题?

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

4 个答案:

答案 0 :(得分:0)

在您的问题查询中,根据以下代码更改while循环:

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

$d1变量作为数组并更改问题的选择查询。

答案 1 :(得分:0)

您的错误在于您从上一次查询迭代中选择问题,并选择章节。

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

将始终选择最后一章。但是你有另一个问题。假设您想在用户从下拉列表中选择一些值时显示问题,则必须使用POST / GET / AJAX将该选择发送回PHP并根据该选择生成结果。像这样:

    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;           
      } 
    }

答案 2 :(得分:0)

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

答案 3 :(得分:0)

$sql1 = "select question from math where chapter=".$d1;