Jump to content

Php quiz


Go to solution Solved by Psycho,

Recommended Posts

Hey guys,

I'm making php quiz, but got some problems with results getting - they are all wrong. Here is the db table for it.

CREATE TABLE IF NOT EXISTS `quiz` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `question` text COLLATE utf8_unicode_ci NOT NULL,
  `answer1` text COLLATE utf8_unicode_ci NOT NULL,
  `answer2` text COLLATE utf8_unicode_ci NOT NULL,
  `answer3` text COLLATE utf8_unicode_ci NOT NULL,
  `correctanswer` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `pollid` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=34 ;

And script itself.

$questionNumber = 1;
$query = sql_query("SELECT * FROM quiz ORDER BY RAND() LIMIT $limit");
while ($result = mysqli_fetch_array($query)) { ?>
	<div class="padding">
	 <p>
	 <?php echo "<b><h4>" . $questionNumber . ") " . $result['question'] . "</h4><br></b>"; ?>
	 <input type="radio" name="answer[<?php echo $result['id'] ?>]" value=""> <?php echo $result['answer1']; ?> <br>
	 <input type="radio" name="answer[<?php echo $result['id'] ?>]" value=""> <?php echo $result['answer2']; ?> <br>
	 <input type="radio" name="answer[<?php echo $result['id'] ?>]" value=""> <?php echo $result['answer3']; ?> <br>
	 </p>
	 <hr>
	 </div>

<?php
$questionNumber +=1;
}
    $correctAnswers = 0;
    $wrongAnswers = 0;
    $questions = '';
    
    $idList = join (',', array_map('intval', array_keys($_POST['answer'])));
    $sql = "SELECT id, question, answer1, answer2, answer3, correctanswer FROM quiz WHERE id IN ($idList)";
    $res = sql_query($sql) ;
    $qno = 1;
    while (list($id, $q, $a, $b, $c, $correct) = mysqli_fetch_row($res)) {
	    if ($correct == $_POST['answer'][$id]) {
		    $correctAnswers +=1;
	    }
	    else {
		    $wrongAnswers +=1;
	    }
    }
echo $correctAnswer;
echo $wrongAnswers;
?>

Help me guys!

Link to comment
https://forums.phpfreaks.com/topic/289375-php-quiz/
Share on other sites

I have made an online quiz https://www.pepster.com/ in php, jQuery and Ajax, but with that said I think you logic is reversed. You should wait on the user's response, may be something like:

<?php
$result['question'] = 'Who portrayed Ferris Bueller in "Ferris Bueller\'s Day off"?';
$result['answer1'] = 'Tom Hanks';
$result['answer2'] = 'Matthew Broderick';
$result['answer3'] = 'Alan Ruck';
?>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?= '<h1>' . $result['question'] . '</h1>'; ?>
<form action="myQuiz.php" method="post">
	 <input type="radio" name="answerA" value="A"> <?php echo $result['answer1']; ?> <br>
	 <input type="radio" name="answerB" vluee="B"> <?php echo $result['answer2']; ?> <br>
	 <input type="radio" name="answerC" value="C"> <?php echo $result['answer3']; ?> <br>
   <input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

Then check against the database table against the user's response.

If you want to get a detail look into how to build a quiz I made a tutorial: http://www.jrpepp.com/tutorialTrivia.php

 

Note: the tutorial doesn't match the actual game play for I have been improving the trivia game myself, but it should give you an idea how to go about building a php quiz.

 

Link to comment
https://forums.phpfreaks.com/topic/289375-php-quiz/#findComment-1483560
Share on other sites

  • Solution

You're not passing a value for the answers! You should have the answers in a separate table with their own ID so you can pass the ID of the answer. With what you have you need to pass the text of the answer which could cause all sorts of problem if you are not escaping the data and handling it both in the output and in the POST data.

Link to comment
https://forums.phpfreaks.com/topic/289375-php-quiz/#findComment-1483562
Share on other sites

You're not passing a value for the answers! You should have the answers in a separate table with their own ID so you can pass the ID of the answer. With what you have you need to pass the text of the answer which could cause all sorts of problem if you are not escaping the data and handling it both in the output and in the POST data.

Can you wrote it in php please. i'm not so good in php :D

Link to comment
https://forums.phpfreaks.com/topic/289375-php-quiz/#findComment-1483567
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.