Jump to content

Recommended Posts

I'm working on this quiz. Something has gone wrong with my form though. The page displays a random question but instead of showing 4 different answer options, it repeats one option 4 times. How do should I go about fixing this?

<?php
SESSION_START();
//Connect to MySQL:
$con = new mysqli("localhost", "root", "", "quizproject");
?>
<html> 
<body> 
<h1>Hello, <?php echo $_SESSION["fname"]; " " ?> <?php echo $_SESSION["lname"]; ?>.</h1>
<p><h2>Quiz Time! Please answer each of the following questions:</h2><br>

<?php
//retrieve question list:
$get_questions = $con->prepare("SELECT question_ID, question FROM questions");
$get_questions->execute();
$get_questions->bind_result($question_ID, $question);

$questions = array();

while ($get_questions->fetch()) {
    
    $questions[$question_ID] = array($question_ID, $question, array());
}

// retrieve answer list:
$get_answers = $con->prepare("SELECT id, question_ID, answers, correct FROM answers");
$get_answers->execute();
$get_answers->bind_result($id, $question_ID, $answers, $correct);

while ($get_answers->fetch()) {
    $questions[$question_ID][2][$id] = array($id, $answers, $correct);
}

// Scramble the array and print:
shuffle($questions);
?>
<form method="get" action="result.php">
<div class="question">
				<label><h2><?php echo $question ?></h2></label>
				<br>
				<input type="hidden" name="question<?php echo $question_ID ?>_id" value="<?php echo $question_ID; ?>" id="question<?php echo $question_ID; ?>_id"/>
				<input name="answer<?php echo $id ?>" id="q<?php echo $id ?>_a1" value="1" type="radio"/>
				<label for="q<?php echo $id ?>_a1"> <?php echo $answers ?></label><br>
				<input name="answer<?php echo $id?>" id="q<?php echo $id ?>_a2" value="2" type="radio"/>
				<label for="q<?php echo $id ?>_a2"> <?php echo $answers ?></label><br>
				<input name="answer<?php echo $answers ?>" id="q<?php echo $id ?>_a3" value="3" type="radio"/>
				<label for="q<?php echo $id ?>_a3"> <?php echo $answers ?></label><br>
				<input name="answer<?php echo $id ?>" id="q<?php echo $id ?>_a4" value="4" type="radio"/>
				<label for="q<?php echo $id ?>_a4"> <?php echo $answers ?></label><br>
			</div>




<?php
echo "<br><input name=\"submit\" type=\"submit\" value=\"Submit\">";
echo "</form></body></html>";
?>

		
</form>
</html>
</body>
Link to comment
https://forums.phpfreaks.com/topic/288296-php-and-forms/
Share on other sites

I wrote a trivia game in Flash and the used a MySQL database, but I have since converted the trivia game over to JQuery, PHP and AJAX. The one thing that has remain constant over all these years is the query string (with minor changes obviously):

$query = 'SELECT  id, question, answerA, answerB, answerC, answerD, correct FROM movieTrivia WHERE id>=:current_id ORDER BY id ASC LIMIT 1';

Your code looks like it doesn't have individual answers, I could be wrong for I just did a quick look over of your code:

 

If you want you can have a look at the code that I wrote for the trivia game at: http://www.jrpepp.com/displayPage.php?page=189

 

and a working script at : http://www.pepster.com/

Edited by Strider64
Link to comment
https://forums.phpfreaks.com/topic/288296-php-and-forms/#findComment-1478552
Share on other sites

Couple things here.

 

First, I may be missing it but I'm not seeing where you're setting the $answers variable. Or the $question variable, for that matter - are you not showing some looping?  In addition, if your script is giving you anything other than an array to string conversion error, you're printing the same string ($answers) over and over again.

 

Your initial queries could be rewritten using a join to be a single call to the database as well. There are a couple different ways to get one random question and it's associated answers without having to load the entire contents of both tables into an array.

Edited by maxxd
Link to comment
https://forums.phpfreaks.com/topic/288296-php-and-forms/#findComment-1478577
Share on other sites

Couple things here.

 

First, I may be missing it but I'm not seeing where you're setting the $answers variable. Or the $question variable, for that matter - are you not showing some looping?  In addition, if your script is giving you anything other than an array to string conversion error, you're printing the same string ($answers) over and over again.

 

Your initial queries could be rewritten using a join to be a single call to the database as well. There are a couple different ways to get one random question and it's associated answers without having to load the entire contents of both tables into an array.

 

no on the looping which is probably a significant issue since it's suppose to output a total of 5 questions.

this is my first crack at working with databases so i'm a little confused and i'm not sure how to use a join.

Link to comment
https://forums.phpfreaks.com/topic/288296-php-and-forms/#findComment-1478580
Share on other sites

Before you do anything with a database, you need to ensure that your table structure is sound. Otherwise, basic operations become difficult to do properly, to say nothing of more complex actions. So, read through this tutorial first. It will get you on the right track, which will then make your queries easier to write.

 

http://mikehillyer.com/articles/an-introduction-to-database-normalization/

Link to comment
https://forums.phpfreaks.com/topic/288296-php-and-forms/#findComment-1478582
Share on other sites

As KevinM1 points out, the first thing you want to do is acquaint yourself with some database theory (nice article, BTW). The table structure you define in your original queries doesn't look bad depending on the content of the 'answers' column in the answers table, but that's outside the scope of this question. Basically, I'd do something along the lines of

SELECT q.question_id
      ,q.question
      ,a.id AS answer_id
      ,a.answers
FROM questions q
LEFT JOIN answers a
    ON q.question_id = a.question_id

If you're feeling real adventurous, you can add an ORDER BY RAND() clause and try to limit the response to one question in order to avoid having to do it in PHP.

Once you've got your record set returned (for this example, I'm assuming in an associative array called $result), try something along the lines of the following:

<h2><?php echo $result['question'] ?></h2>
<input type="hidden" name="question_id" value="<?php echo $result['question_ID']; ?>" id="question<?php echo $result['question_ID']; ?>_id"/>
<?php
foreach( $result as $answer ){
    echo "<label for='q{$answer['[answer_id']}'>{$answer['answers']}</label>";
    echo "<input name='answer_{$answer['question_id']}' id='q{$answer['answer_id']}' value='{$answer['answer_id']}' type='radio'/>";
}
?>

As an aside, I wouldn't bother selecting the 'a.correct' column while ouputting the question and answer - you're not going to need that information until you grade the answer (after form submission). Also, I should think it goes without saying, but this won't work if you want to display more than 1 question at a time, but you get the general idea I hope.

Edited by maxxd
Link to comment
https://forums.phpfreaks.com/topic/288296-php-and-forms/#findComment-1478592
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.