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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度