普通网友 2015-03-07 03:06
浏览 73

如何在PHP代码中从数据库中检索数据

There are a lots of error in my code but I don't know which one of the error.

I'm trying to retrieve data from database and paste it into html but there are problem reading table. There are two tables from database one of it are diagnostic table and another one is diagnostic answer. The table of diagnostic contain 10 question and each question had 3 multiple choices answer that I put in diagnostic answer table. I'm also try to loop the question from the diagnostic table same as the answer.

<form name="question" action="diagnostic_test_process.php" method="post">`

<fieldset>
<table><?PHP      

$sql=mysql_query("SELECT * FROM diagnostic")
or die ("Problem reading table:". mysql_error());
while ($result = mysql_fetch_array ($sql)){ ?>

<tr>
   <td colspan="2">
   <label for="question"></label>
      <?PHP                                
        echo "<p>".$result["id_diagnostic"].".";
        echo $result["question"]."</p>"; ?>
   </td>
</tr>

 <?php
 $sql=mysql_query
 ("SELECT a.id_answer, a.answer FROM diagnostic_answer a, diagnostic b WHERE a.id_diagnostic = b.id_diagnostic and a.id_diagnostic= " . $result["ïd_diagnostic"])          
 or die ("Problem reading table:". mysql_error());
 while ($value= mysql_fetch_array ($sql)){  ?>

<tr>
   <td>
     <label for="answer"></label>
     <p><input id=<?php echo $answer['id_answer']; ?> type='radio' name='answer' value='1' onClick="calculate()">
     <?php echo $value['answer']; ?></p>
   </td>
</tr>

<?php } 
// close recordset
?>
<?php } ?>

    </table>
</fieldset>
  • 写回答

2条回答 默认 最新

  • duandaijian1583 2015-03-07 03:40
    关注

    I tried to edit some of your code, see the code and tried to figure it out. you should go to youtube search some tutorial for reference.

    <form name="question" action="diagnostic_test_process.php" method="post">`
    
    <fieldset>
    <table><?PHP      
    mysql_fetch_array ($sql);//This is the issue, don't know what you want
    
    //$result need to be defined here
    
    $sql=mysql_query("SELECT * FROM diagnostic"
    while $result = put a defined string here ); 
    
    include the html inside the php loop
    
    echo "<tr>"; //do like this for all below html code
       <td colspan="2">
       <label for="question"></label>
          <?PHP                                
            echo "<p>".$result["id_diagnostic"].".";
            echo $result["question"]."</p>"; ?>
       </td>
    </tr>
    
      
      ?>
      
      
      
     <?php
     $sql=mysql_query
     ("SELECT a.id_answer, a.answer FROM diagnostic_answer a, diagnostic b WHERE a.id_diagnostic = b.id_diagnostic and a.id_diagnostic= " . $result["ïd_diagnostic"])          
     or die ("Problem reading table:". mysql_error());
     while ($value= mysql_fetch_array ($sql)){  ?>
    
    <tr>
       <td>
         <label for="answer"></label>
         <p><input id=<?php echo $answer['id_answer']; ?> type='radio' name='answer' value='1' onClick="calculate()">
         <?php echo $value['answer']; ?></p>
       </td>
    </tr>
    
    <?php } 
    // close recordset
    ?>
    <?php } ?>
    
        </table>
    </fieldset>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题