โค้ด php สร้างแบบทดสอบ (อาเรย์)

พอดีผมทำไฟล์ 2 ไฟล์ เป็น ฟอร์ม และ ตรวจคำตอบพร้อมเก็บคะแนนไว้ในฐานข้อมูล  แต่ยังเกิดปัญหาคือ เวลาตรวจข้อสอบ มันจะตรวจเป็น 0 คะแนน    เท่าที่ผมดูผมคิดว่ามันน่าจะไม่มีการส่งค่า คำตอบ (เป็นอาเรย์) มาเพื่อตรวจสอบในหน้าตรวจข้อสอบ เลยอยากให้เพื่อนๆ ช่วยดูทีครับ

ไฟล์ pretest.php


  <table width="80%" border="0" cellspacing="0" cellpadding="5">
    <tr>
      <td bgcolor="#CCFF33">
      
                                        <form name="form1" method="post" action="check_pretest.php">
                            <?
                                require('dbconnect.php');
                                $tb="tb_pretest";
                                $sql="Select * From $tb order by rand() limit 10";
                                $db_query=mysql_query($sql);
                                $i=0;
                                while($result=mysql_fetch_array($db_query))
                                {
                                $i++;
                            ?>
                                    <table width="64%" border="0" align="center">
                                      <tr>
                                        <td colspan="5">
                                          
                                          <div align="left">
                                            ข้อที่ : <? echo"$i"; ?>
                                            <input name="id[<?=$i;?>]" type="hidden" value="<?=$result["id"];?>">
                                            <?=$result["question"];?>
                                        </div></td>
                              </tr>
                                      <tr>
                                        <td width="16%"></td>
                              <td align="left">
                                <input name="c<?=$i;?>" type="radio" value="1" />
                                <?=$result["c1"];?>                                </td>
                            </tr>
                                      <tr>
                                        <td></td>
                              <td align="left">
                                <input type="radio" name="c<?=$i;?>" value="2" />
                                <?=$result["c2"];?>                                </td>
                            </tr>
                                      <tr>
                                        <td></td>
                              <td align="left">
                                <input type="radio" name="c<?=$i;?>" value="3" />
                                <?=$result["c3"];?>                                </td>
                            </tr>
                                      <tr>
                                        <td></td>
                              <td width="84%" align="left">
                                <input type="radio" name="c<?=$i;?>" value="4"/>
                                <?=$result["c4"];?>
                                <input name="answer[<?=$i;?>]" type="hidden" value="<?=$result["answer"];?>" />       </td>
                            </tr>
                                    </table>
                          <hr align="center"  width="70%"/>
                                    <?
                                    //echo $result['answer'];
                        }
                        mysql_close();
                        ?>

                                    <div align="center">
                                      <br>
                                      <input type="submit" name="Submit" value="ตรวจคะแนน" >
                                    </div>
                                  </form>
      
      </td>
    </tr>
  </table>


ไฟล์ check_pretest.php

<?
                            $score=0;
                            for($i=1;$i<=count($id);$i++)
                            {
                              $ch="c$i";
                              if($$ch==$answer[$i])
                              {
                                $score=$score+1;
                              }
                            }

                                require('dbconnect.php');
                                $tb="tb_score_pretest";
                            
                                mysql_query("INSERT INTO $tb (username, score) VALUES('$ss','$score')") or die ("<center>คุณทำข้อสอบ Pre Test ไปแล้ว<br>
                                                                                                                
                                <br><br>
                                <a href='test.php'>กลับไปที่เมนู</a><br><br>");
                                    
                                mysql_close($connect);
?>
แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่