2013-08-19 1 views
3

PHP와 MySQL을 사용하여 퀴즈 앱을 준비 중입니다. 라디오 버튼의 유효성 검사에 문제가 있습니다.PHP를 사용하여 퀴즈 시스템에서 선택된 라디오 버튼의 유효성을 검사하는 방법

체크 버튼을 클릭하면 첫 번째 옵션이 올바른지 여부 만 확인되지만 실제로 라디오 버튼의 선택된 옵션이 올바른지 확인해야합니다.

내가 사용한 코드는 여기에 있습니다 :

$singleSQL = mysql_query("SELECT * FROM questions WHERE id='$question' LIMIT 1"); 
while($row = mysql_fetch_array($singleSQL)){ 
    $id = $row['id']; 
    $thisQuestion = $row['question']; 
    $type = $row['type']; 
    $subject =$row['subject']; 
    $exam =$row['exam']; 
    $explan =$row['explan']; 
    $question_id = $row['question_id']; 
    $s ='<strong>'.$subject.'</strong>'; 
    $e ='<small>'.$exam.'</small>'; 
    $q = '<h2>'.$thisQuestion.'</h2>'; 
    $ex ='<div id="welcomeDiv" style="display:none;" class="expl" >'.$explan.'</div>'; 
    $sql2 = mysql_query("SELECT * FROM answers WHERE question_id='$question' ORDER BY rand()")or die(mysql_error()); 

    while($row2 = mysql_fetch_array($sql2)){ 
     $id2=$row2['id']; 
     $answer = $row2['answer']; 
     $correct = $row2['correct'];  
     $answers .= '<table class="table table-hover table-bordered"> 
        <tr> 
        <td class="chk"> 
        <label style="cursor:pointer;"> 
        <input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label></td> 
      </tr> 
       </table> 
     <input type="hidden" id="qid" value="'.$id.'" name="qid"><br />'; 

    $result=mysql_query("SELECT id FROM answers WHERE question_id='$question' "); 
    $nrows=mysql_num_rows($result); 
    for($i=0;$i<=4;$i++){ 
     if (isset($_POST[$correct])) { 
      $answer= $_POST[$correct]; 
     } 

     if($answer&&$correct==1){ 
      echo $dv.='<div style="display:none;" class="green" id="chek" >Your answer '.$answer.' is correct</div>'; 
     } else { 
      echo $dv2.='<div style="display:none;" class="red" id="chek" >Your answer '.$answer.' is worng</div>';} 
     } 

내가 한 질문 두 개의 테이블이 있고 anothher 대답

질문 테이블

  id question_id question 
      1 1   question1 

답변 테이블

  id questions_id answer correct 
      1  1  op1 1 
      2  1  op2 0 
      3  1  op3 0 
      4  1  op4 0 
입니다
+3

이없이 밸브와 실린더 헤드이다. – DevlshOne

+0

두 개의 내부 쿼리가 필요하지 않습니다. 실제로는 사실 좋지 않습니다. – Prix

+1

제안, [** 새 코드 **에서 mysql_ * 함수를 사용하지 마십시오 (http://bit.ly/phpmsql). 그들은 더 이상 유지되지 않으며 [공식적으로 사용되지 않습니다] (http://j.mp/XqV7Lp). [** 빨간색 상자 **] (http://j.mp/Te9zIL)를 참조하십시오. 대신 [* prepared statements *] (http://j.mp/T9hLWi)에 대해 알아보고 [PDO] (http://php.net/pdo) 또는 [MySQLi] (http://php.net/)를 사용하십시오. mysqli) - [이 기사] (http://j.mp/QEx8IB)는 어떤 결정을 내리는 데 도움이 될 것입니다. PDO를 선택하면 [여기는 좋은 튜토리얼입니다] (http://j.mp/PoWehJ). 명확성을 위해 –

답변

0

name = "rads"는 모든 답변 그룹에 고유 한 이름을 지정해야하는 문제입니다.

은이 같은 것을 추가해야합니다 :

HTML 마크 업없이
$i = 1; 
while($row2 = mysql_fetch_array($sql2)){ 
     $id2=$row2['id']; 
     $answer = $row2['answer']; 
     $correct = $row2['correct'];  
     $answers .= '<table class="table table-hover table-bordered"> 
        <tr> 
        <td class="chk"> 
        <label style="cursor:pointer;"> 
        <input type="radio" name="rads_'.$i.'" value="'.$correct.'">'.$answer.'</label></td> 
      </tr> 
       </table> 
     <input type="hidden" id="qid" value="'.$id.'" name="qid"><br />'; 
$i++; 
관련 문제