2013-08-13 2 views
1

내 데이터베이스에는 질문 용 2 개와 답변 용 2 개가 있습니다.클릭하여 답변을 확인하고 답변을 표시하는 방법

이제 문제는 내가 답을 클릭하면 제출 여부에 상관없이 올바른지 아닌지 div의 동일한 페이지에 표시됩니다. 나는 그것을 위해 노력하고

<?php 
    session_start(); 
    require_once("scripts/connect_db.php"); 
    $arrCount = ""; 

    if(isset($_GET['question'])){ 
    $question = preg_replace('/[^0-9]/', "", $_GET['question']); 
    $output = ""; 
    $answers = ""; 
    $q = ""; 
    $dv=""; 
    $dv2=""; 


    $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()"); 
     while($row2 = mysql_fetch_array($sql2)){ 
      $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 /> 
      '; 

      $answer=$_POST[$correct]; 
      if(isset($answer)&&$correct==1){ 

      echo $dv.='<div style="display:none; color=green;" id="welcomeDiv" id="dv">Your answer '.$answer.' is correct</div>';} 

      else{ 
       $dv2.='<div style="display:none; color=red;" id="welcomeDiv" id="dv2">Your answer '.$answer.' is worng</div>';} 



     } 
     $output = ''.$s.','.$e.''.$q.','.$dv.''.$dv2.''.$answers.''.$ex.'<input type="button" name="answer" value="check" onclick="showDiv();check_asnwer()" id="" />'; 
     echo $output; 
     } 
    } 


    ?> 

그러나 그것은 나에게이 같은 오류 보여줍니다 :

코드는 여기

 Notice: Undefined index: 0 in F:\wamp\www\quiz\questions.php on line 38 
    Notice: Undefined index: 1 in F:\wamp\www\quiz\questions.php on line 38 
    Notice: Undefined index: 0 in F:\wamp\www\quiz\questions.php on line 38 
    Notice: Undefined index: 0 in F:\wamp\www\quiz\questions.php on line 38 

내 코드에 문제는 내가 필요 해요 좀 도와주세요 거기를 내 프로젝트를 끝내기 위해서. 사전

+0

mysql_function 대신 Prepared Statement를 사용하여 PDO 또는 MySQLi를 사용해야합니다. 사용하지 않으려합니다. – Fredd

+0

정확성을 검사 할 예정인 루프 안에서'$ answer'의 값을 변경하는 것처럼 보입니다. 이것은 실수입니까 아니면 매우 이상한 코드 작성입니까? – DevlshOne

답변

0
 $answer=$_POST[$correct]; 
     if (isset($answer)) { 

에서

감사는

 if (isset($_POST[$correct])) { 
      $answer = $_POST[$correct]; 

그런 다음, 정의되지 않은 값에서 지정 병동 후 존재 여부를 테스트하려고 할 수 있어야한다. 존재를 위해 $ _POST 값을 직접 테스트 한 다음 실제로 존재하는지 확인한 후에 할당해야합니다.

+0

여전히 내 문제 dint는 코드를 tryed하지만 그것은 단지 coret 답변을 wheni 잘못된 옵션을 클릭했을 때 똑같은 것을 보여주고있다. – user2676707

관련 문제