Jump to content

Help with correcting php quiz


Maz_2014

Recommended Posts

Could anyone help me please. I am trying to create a quiz in (quiz.php) which is works fine, but when I am trying to correct the quiz in another page (result.php) I had an error. I think the problem with posted answers and I couldn't solve it.

 

This is my code in (quiz.php):

<form method="post" action="result.php">
<?php 
session_start();

$questionNumber = 1;

$query = mysql_query("SELECT * FROM Question WHERE TID = 'TEST' order by RAND()");
while ($result = mysql_fetch_array($query)) { ?>

	 <?php echo $questionNumber . ") " . $result['Question'] . "<br>"; ?>
	 <input type="radio" name="answer<?php echo $result["QID"] ?>" value="A"> <?php echo $result['A']; ?> <br>
	 <input type="radio" name="answer<?php echo $result["QID"] ?>" value="B"> <?php echo $result['B']; ?> <br>
	 <input type="radio" name="answer<?php echo $result["QID"] ?>" value="C"> <?php echo $result['C']; ?> <br>
	 <input type="radio" name="answer<?php echo $result["QID"] ?>" value="D"> <?php echo $result['D']; ?> <br>

<?php
$questionNumber +=1;
}
?>
	  <input type="submit" name="checkQuiz" value="Check Quiz" onclick="location.href='result.php'" />
      
</form>

And This is my code in (result.php):

<?php

if(isset($_POST['checkQuiz'])) {

$correctAnswers = 0;
$wrongAnswers = 0;



$idList = join (',', array_map('intval', array_keys($_POST['answer'])));

 $sql = "SELECT QID, Answers FROM correct WHERE QID IN ($idList)";

while (list($QID, $correct) = mysql_fetch_row($res)) {

if ($correct == $_POST['answer'][$QID]) {
		 $correctAnswers +=1;
}
else {
		 $wrongAnswers +=1;
}
}
}

?>

// in the body

<?php 

$numberOfQs = $correctAnswers + $wrongAnswers;
$score = round(($correctAnswers / $numberOfQs) * 100);
?>

Correct Answers: <?php echo $correctAnswers; ?> <br>
Wrong Answers: <?php echo $wrongAnswers; ?> <br>
Score: <?php echo $score; 

?>
Link to comment
Share on other sites

Well I can see a couple things right off the bat.  Where are you defining $QID for this part?

list($QID, $correct)

And $res would be undefined since you haven't even ran the query, you only defined the query string with $sql.

Link to comment
Share on other sites

Dear fastsol, I think this is what you mean:

<?php
if(isset($_POST['checkQuiz'])) {

$correctAnswers = 0;
$wrongAnswers = 0;



$idList = join (',', array_map('intval', array_keys($_POST['answer'])));

 $sql = "SELECT QID, Answers FROM correct WHERE QID IN ($idList)";

while($res = mysql_fetch_array( $sql ))
{

	$correct = $res['Answers'];
	$QID = $res['QID'];

if ($correct == $_POST['answer'][$QID]) {
		 $correctAnswers +=1;
}
else {
		 $wrongAnswers +=1;
}
}
}

?>

But still the same problem!!

Link to comment
Share on other sites

 BTW - why do you have an onclick to direct you to the result.php script as well as a form directing the submission to the same script?  The onclick is not needed, and depending on whether it happens before the actual form submission, it may cause you problems in result.php.

Link to comment
Share on other sites

When you have assured yourself that you are using the proper syntax  :) then the problem must lie with your data.

 

If you had validated your input before trying to use it you would have caught this error.  There is NO element  $_POST['anwer'].  You do have an element in the POST array named "answer?" where ? is the value of your QID column.

Link to comment
Share on other sites

Your code is not what you think it is.  Your name attribute is the string "answer" followed immediately by the value of your QID column from your query.  So if QID for that row is "123" then your name is "answer123".  So to extract it form the post array you would have to use "$_POST['answer123'].

 

But all of this is meaningless since the whole point of radio buttons is to provide a set of choices from which the user may only choose ONE option.  This will not produce an array in $_POST, but merely a single value so your very complicated code to manage the returned value from the user is unnecessary.

Link to comment
Share on other sites

You need to assemble your quiz form so that the answers relate to a question number.  Your attempt so far is completely wrong since you don't do that and since you are trying to gather the correct answers using the values of the selected answers from the answer table, rather than the question #.

 

If you are going to write code, learn how to (try to) separate your html from your php as much as possible.  Example:  You begin your first piece of code with an html form tag, and then jump into php mode.    Silly.  Instead - do your logic, process your input, get your data, THEN build your output

Edited by ginerjm
Link to comment
Share on other sites

Dear fastsol, I think this is what you mean:

<?php
if(isset($_POST['checkQuiz'])) {

$correctAnswers = 0;
$wrongAnswers = 0;



$idList = join (',', array_map('intval', array_keys($_POST['answer'])));

 $sql = "SELECT QID, Answers FROM correct WHERE QID IN ($idList)";

while($res = mysql_fetch_array( $sql ))
{

	$correct = $res['Answers'];
	$QID = $res['QID'];

if ($correct == $_POST['answer'][$QID]) {
		 $correctAnswers +=1;
}
else {
		 $wrongAnswers +=1;
}
}
}

?>

But still the same problem!!

You're still not running the query!!  You need to call mysql_query() before the while loop.

Link to comment
Share on other sites

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.