Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
    
how to do the recordset..anyone know? ;) –  candy Mar 7 at 3:07
1  
i don't know what you are asking. –  Dagon Mar 7 at 3:15
    
Welcome to this site. Please tell us what is the problem with your code (comparing what you expected, and what you got), and if you have got error messages. This will improve your question, and help others help you. The Help section may be a good start. –  mins Mar 7 at 3:35

2 Answers 2

the error is here.

while ($result = mysql_fetch_array ($sql)){

it should be

while ($result = mysql_fetch_array ($sql, MYSQL_ASSOC)){

or

while ($result = mysql_fetch_assoc ($sql)){

don't forget to do the same for the 2nd while loop you have in there

share|improve this answer
    
thank you for helping :) –  candy Mar 7 at 6:30

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>

share|improve this answer
    
thank you for helping :) –  candy Mar 7 at 6:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.