Arcman20 Posted July 2, 2014 Share Posted July 2, 2014 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! Quote Link to comment Share on other sites More sharing options...
Strider64 Posted July 2, 2014 Share Posted July 2, 2014 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. Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted July 2, 2014 Solution Share Posted July 2, 2014 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. Quote Link to comment Share on other sites More sharing options...
Arcman20 Posted July 2, 2014 Author Share Posted July 2, 2014 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 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 2, 2014 Share Posted July 2, 2014 This forum is for helping people with their code, not writing it for them. If you need a programmer to do the work for you, hire one. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 2, 2014 Share Posted July 2, 2014 This forum is for helping people with their code, not writing it for them. If you need a programmer to do the work for you, hire one. Yep, looks like a homework assignment to me. Quote Link to comment Share on other sites More sharing options...
Arcman20 Posted July 2, 2014 Author Share Posted July 2, 2014 Yep, looks like a homework assignment to me. , i have bad english so i misunderstood you. Figured out myself thanks anyway Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.