1

我想使用 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

4 回答 4

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变量设为数组并更改问题的选择查询。

于 2013-08-28T11:21:27.140 回答
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;           
      } 
    }
于 2013-08-28T11:34:53.303 回答
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;           
         } 
    }
  ?>
于 2013-08-28T11:47:26.917 回答
0
$sql1 = "select question from math where chapter=".$d1; 
于 2013-08-28T11:59:38.190 回答