douchuoxuan3177 2013-08-28 11:16
浏览 45
已采纳

通过复选框从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;           
      } 
  ?>
  • 写回答

4条回答 默认 最新

  • drr7731 2013-08-28 11:34
    关注

    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;           
          } 
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测