Jump to content

Recommended Posts

Hello,

I am trying to build a quiz with PHP and MySQL. I have successfully managed to build the database and display the questions from the database. I have also managed to get the radio button to individually map to each question (rather than seeing all questions with one possible answer). What I have been unable to do (for some days now) is to match the correct radio button with the answer (all say incorrect at the moment even if you answer correctly.) Any help would be greatly appreciated.

Code below

 

questions.php

<?php
  
require_once('connectvars1.php');

//Connect to the database
        $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name);


$query = "SELECT * FROM questions"; 
$result = mysqli_query($dbc, $query); 


// if records are present 
if (mysqli_num_rows($result) > 0) { 
$row = mysqli_fetch_array($result); 

if (mysqli_num_rows($result) > 0) { 

// print answer list as check boxes 
while ($row = mysqli_fetch_array($result)) { 
$id = $row['autokey'];
$quest = $row['q_text'];
$option1 = $row['op1'];
$option2 = $row['op2'];
$option3 = $row['op3'];
$option4 = $row['op4'];
$answer= $row['answer'];
echo "$quest";
?>
<br />
<input type= radio name= <?php echo "$id" ?> <value="A"/>A. <?php echo "$option1" ?><br />


<input type= radio name= <?php echo "$id" ?> <value="B"/>B. <?php echo "$option2" ?><br />


<input type= radio name= <?php echo "$id" ?> <value="C"/>C. <?php echo "$option3" ?><br />


<input type= radio name= <?php echo "$id" ?> <value="D"/>D. <?php echo "$option4" ?><br />

<br />

<?PHP

} 
?>

<br />
<div align="center"><input type="submit" name="btn_submit" value="Submit Answers" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>

<?php


} 

echo '</form>'; 
} 




mysqli_close($dbc);
?> 

 

score.php

 

<?php 


// Check  answers
if((isset($_POST['$id'])) && ($_POST['$id'] == "$answer")){
echo 'Your answer was correct for q1!';
}else{
echo 'Your answer was incorrect for q1.';
}
?>
<br />
<?php
if((isset($_POST['$id'])) && ($_POST['$id'] == "$answer")){
echo 'Your answer was correct for q2!';
}else{
echo 'Your answer was incorrect for q2.';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/234659-quiz-code-please-help/
Share on other sites

You'll want to name your radios buttons as question[$id], eg

<input type="radio" name="question[<?php echo $id; ?>]" value="A"/>A. <?php echo $option1 ?><br />
<input type="radio" name="question[<?php echo $id; ?>]" value="B"/>B. <?php echo $option2 ?><br />
<input type="radio" name="question[<?php echo $id; ?>]" value="C"/>C. <?php echo $option3 ?><br />
<input type="radio" name="question[<?php echo $id; ?>]" value="D"/>D. <?php echo $option4 ?><br />

 

Now $_POST['question'] will be a multi-dimensional array containing all the users answers to the quiz

 

Now in score.php you logic completly wrong here. Variables do not get passed from page to page. So the variable $answer will not exits in this page. What you will have to do in score.php is connect to your database. Run an SQL query and grab all the answers and place them in an array.

// get all answers and add them to an array
$query = 'SELECT autokey, answer FROM questions';
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))
{
    $question_id = $row['autokey'];
    $answer = $row['answer'];

    $question_answers[$question_id] = $answer;
}

 

Now we have the array of answers. We can now use a loop and compare the users answers. Like so

// loop through the posted ansers and compare
foreach($question_answers as $question_id => $question_answer)
{
    if(isset($_POST['question'][$question_id])
    {
        $user_answer = $_POST['question'][$question_id];
        if($user_answer == $question_answer)
        {
            echo "<p>Your answer was correct for Q{$question_id}!<p>";
        }
        else
        {
            echo "<p>Your answer was incorrect for Q{$question_id}.<p>";
        }
    }
    else
    {
        echo "<p>You didn't give an anser for Q{$question_id}.<p>";
    }
}

Thanks very much for the help!

 

There is a poblem still with score.php. I get the following error:

 

Parse error: syntax error, unexpected '{' in line 28

 

Why would this be?  I have tried swappiing the { around and deleting with the same result

 

Help, as always, appreciated.

 

Score.php


<?php
  
require_once('connectvars1.php');

//Connect to the database
        $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name);

// get all answers and add them to an array
$query = 'SELECT autokey, answer FROM questions';

$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))

{    
$question_id = $row['autokey'];    

$answer = $row['answer'];    

$question_answers[$question_id] = $answer;
}

// loop through the posted ansers and compare

foreach($question_answers as $question_id => $question_answer)
{
if(isset($_POST['question'][$question_id])    
       {
$user_answer = $_POST['question'][$question_id];        
if($user_answer == $question_answer)        
{            
	echo "<p>Your answer was correct for Q{$question_id}!</p>";        
}        
else        
{          
	echo "<p>Your answer was incorrect for Q{$question_id}.</p>";        

}
}
   
    else    
{        

echo "<p>You didn't give an anser for Q{$question_id}.</p>";    
}

} 
?>

I hope this helps...

 

(Look in the code, I added a comment)

 

Thanks very much for the help!

 

There is a poblem still with score.php. I get the following error:

 

Parse error: syntax error, unexpected '{' in line 28

 

Why would this be?  I have tried swappiing the { around and deleting with the same result

 

Help, as always, appreciated.

 

Score.php


<?php
  
require_once('connectvars1.php');

//Connect to the database
        $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name);

// get all answers and add them to an array
$query = 'SELECT autokey, answer FROM questions';

$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))

{    
$question_id = $row['autokey'];    

$answer = $row['answer'];    

$question_answers[$question_id] = $answer;
}

// loop through the posted ansers and compare

foreach($question_answers as $question_id => $question_answer)
{
if(isset($_POST['question'][$question_id])    
       {
     $user_answer = $_POST['question'][$question_id];        
     if($user_answer == $question_answer)        
     {            
	     echo "<p>Your answer was correct for Q{$question_id}!</p>";        
     }        
     else        
     {          
	     echo "<p>Your answer was incorrect for Q{$question_id}.</p>";      
             // you forgot to close here  
     }
            // you forgot to close here
}
}
   
    else    
{        

echo "<p>You didn't give an anser for Q{$question_id}.</p>";    
}

} 
?>

This still gives the same error:

 

Parse error: syntax error, unexpected '{ on line 20

 

in fact there are more } than { in this solution.

 

I think the problem is in score.php but I will include the updated questions.php if this is involved.

 

Help much appreciated

 


<?php   
require_once('connectvars1.php');

//Connect to the database        
$dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name);

// get all answers and add them to an array
$query = 'SELECT autokey, answer FROM questions';
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{    	
	$question_id = $row['autokey'];    	
	$answer = $row['answer'];    	
	$question_answers[$question_id] = $answer;
}
// loop through the posted ansers and compare
foreach($question_answers as $question_id => $question_answer)
{	
if(isset($_POST['question'][$question_id])           
     {	     
	$user_answer = $_POST['question'][$question_id];        	     
	if($user_answer == $question_answer)        	     
	    {        		     
		echo "<p>Your answer was correct for Q{$question_id}!</p>";        	     
	}        	     
	else        	     
	{          		     echo "<p>Your answer was incorrect for Q{$question_id}.</p>";                   
	// you forgot to close here  	     
	}           
	// you forgot to close here	

}      
 else    	
	{        			
	echo "<p>You didn't give an answer for Q{$question_id}.</p>";    	
	}	
}
?>

 

quiz.php


<?php   
require_once('connectvars1.php');

//Connect to the database        
$dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name);

// get all answers and add them to an array
$query = 'SELECT autokey, answer FROM questions';
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{    	
	$question_id = $row['autokey'];    	
	$answer = $row['answer'];    	
	$question_answers[$question_id] = $answer;
}
// loop through the posted ansers and compare
foreach($question_answers as $question_id => $question_answer)
{	
if(isset($_POST['question'][$question_id])           
     {	     
	$user_answer = $_POST['question'][$question_id];        	     
	if($user_answer == $question_answer)        	     
	    {        		     
		echo "<p>Your answer was correct for Q{$question_id}!</p>";        	     
	}        	     
	else        	     
	{          		     echo "<p>Your answer was incorrect for Q{$question_id}.</p>";                   
	// you forgot to close here  	     
	}           
	// you forgot to close here	

}      
 else    	
	{        			
	echo "<p>You didn't give an answer for Q{$question_id}.</p>";    	
	}	
}
?>

well spotted, thank you very much!

 

It is working now. I had to make a few other changes that I worked out for myself:  while ($row = mysql_fetch_assoc($result)) to while ($row = mysqli_fetch_assoc($result)).

 

Sorry I have one other quetion for the code. THe first item in the database is not displayed. I'm sure there is a simple answer but am not sure what it is!

 

Help, as before, much appreciated

 

quiz.php


<?php
  
require_once('connectvars1.php');

//Connect to the database
        $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name);


$query = "SELECT * FROM questions"; 
$result = mysqli_query($dbc, $query); 


// if records are present 
if (mysqli_num_rows($result) > 0) { 
$row = mysqli_fetch_array($result); 

if (mysqli_num_rows($result) > 0) { 

// print answer list as check boxes 
while ($row = mysqli_fetch_array($result)) { 
$id = $row['autokey'];
$quest = $row['q_text'];
$option1 = $row['op1'];
$option2 = $row['op2'];
$option3 = $row['op3'];
$option4 = $row['op4'];
$answer= $row['answer'];
echo "$quest";
?>
<br />

<input type="radio" name="question[<?php echo $id; ?>]" value="A"/>A. <?php echo $option1 ?><br />

<input type="radio" name="question[<?php echo $id; ?>]" value="B"/>B. <?php echo $option2 ?><br />

<input type="radio" name="question[<?php echo $id; ?>]" value="C"/>C. <?php echo $option3 ?><br />

<input type="radio" name="question[<?php echo $id; ?>]" value="D"/>D. <?php echo $option4 ?><br />

<br />

<?PHP

} 
?>

<br />
<div align="center"><input type="submit" name="btn_submit" value="Submit Answers" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>

<?php


} 

echo '</form>'; 
} 




mysqli_close($dbc);
?> 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.