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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀