Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • how to choose correct answers from multiple choices questions ?

    • 0
    • 0
    • 0
    • 4
    • 0
    • 0
    • 0
    • 563
    Answer it

    how to choose correct answers from multiple choices questions using php and mysql
    i don't know if i'm doing in the right way because i'm still new in programming world 

    1- i'm using an array to store the questions 
    2- another array for answers 
    3- passing the question id and answer id to the processing page then comparing them with the values in data base to check for the correct answer 

    my problem is when i pass the id's into my processing page i only got an id for one question even if i choose different question it's still repeating the same question id 

     

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    		<title> </title>
    		<link rel="stylesheet" type="text/css" href="style.css" />
    	</head>
    
    
    
    
    	<body>
    		<div id="header">
    		
    		<div class="container">
    		
    		<div id="logoArea"></div>
    		 <form action="process.php" method="post" >
    		 	
    <?php 
    require ("mysql_connect.php");
    
    $test_id = $_GET['test_id'];
    
    $qid = array();
    $sql ="Select * from question where test_id='$test_id'";
    $res = mysqli_query($connection, $sql);
    $noq = mysqli_num_rows($res);
    while ($row = mysqli_fetch_assoc($res) ) {
    
    $question_id = $row['question_id'];
    $qid [] = $question_id;
    $questions = array ($row['question_text']) ;
    
         foreach ($questions as $question) {
         	echo "<hr />";
    	echo"<label><p>$question</p></label>"  ;
         }
          	
      /*
      * Get choices
      */
      	  
      $sql1 = mysqli_query($connection,"SELECT * FROM `answers` WHERE answers.question_id =  '". $question_id."' ") or die (mysqli_error($connection));
      while ($row1 = mysqli_fetch_assoc($sql1)) {
      	   $answer_id =$row1 ['answer_id'];
           $answers =array ($row1['answer_text']);
        
         foreach ($answers as $answer) {
         	
         
    		 
    
    	?>
        <label><p><input type='radio' name='answers'   value= '<?php echo $row1 ['answer_id']; ?>' id='answers' /> <?php echo  $answer ; ?> </p></label>
    	<?php
      
    }
    
      }
     
      
    }
     var_dump($qid );
    
    ?>
    
    		 	<input type="submit" name="submit" value=" "/>
    		 	<input type="hidden" name="question_id" value="<?php  echo $question_id  ;    ?>" />
    		 </form>
    		 <div id="navmenu"></div>
    		
    		</div>
    		
    		</div>
    	</body>
    </html>
    <?php
    
    
    include ("mysql_connect.php");
    
    if(isset($_POST["submit"])){
    $correct_answers = 0;
    $wrong_answers = 0;
    $total = 0 ;
    
    $question_id = $_POST['question_id'];
    $selected_choice = $_POST['answers'];
    
    
    
    $sql = mysqli_query($connection, "SELECT * FROM `answers` WHERE  answers.question_id = '".$question_id ."' AND answers.correct = 1  ")or die(mysqli_error($connection));
    while ($result = mysqli_fetch_assoc($sql)){
    
       $correct_choices = array($result['answer_id']);
        
    
    }
    
    
    
    foreach( $correct_choices as  $correct_choice){
    	   $total  ++ ;
    	if($correct_choice == $selected_choice){
    		$correct_answers ++ ;
    	}else{
    		$wrong_answers ++;
    	}
      $score = $correct_answers / $total ;
      $score = number_format($score * 100);
      echo "your score is $score <br  />";
    
    
    }
    
     var_dump($correct_choice); 
      var_dump($_POST); 
           if ($correct_answers > 0){ 
               echo "<h2><span>You have  $correct_answers  correct answers</span></h2>";
    	   }
            if ($wrong_answers > 0) { 
               echo "<h2><span>You have  $wrong_answers  wrong answers</span></h2>" ;
              
            }
    
    
    
      }
    
    
    
    ?>

    my Database 

     

    CREATE TABLE `answers` (
      `answer_id` int(11) UNSIGNED NOT NULL,
      `answer_text` text NOT NULL,
      `question_id` int(11) NOT NULL,
      `correct` tinyint(1) UNSIGNED NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    CREATE TABLE `question` (
      `question_id` int(11) UNSIGNED NOT NULL,
      `question_text` text NOT NULL,
      `test_id` int(11) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

     

 4 Answer(s)

  • Hi Nasyia

    I have checked your code, the reason behind your problem for getting same question id is the hidden field (name : question_id) on line 70 which you are using for storing question_id. In your code the hidden field storing the last question id only because it's out of while loop (used for questions).

    Another thing which needs to be changed is the name of radio buttons, it is same for all options of each question. So you can check answer for only one question with it. To check answers for multiple questions you have to define different id's to radio button group for each question(example: name='answers1'). You can group radio names with question id.

    I have edited the above code and made some changes. You can check code in the attached files.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: