Thezlehman
Thezlehman

Reputation: 11

Transfer Mysql info from a page to another page

I am working on a trivia concept for a website of mine. All the question data is stored on a mysql database. I use the following code for the user to submit the answer of the trivia which is randomized.

<html> 
 <title>Trivia</title>
 <body>
 <h1>Trivia</h1>





<?php 

 mysql_connect("localhost", "trivia", "<snip>") or die(mysql_error()); 
 mysql_select_db("trivia") or die(mysql_error()); 



 $query = 'SELECT * FROM questions ORDER BY RAND() LIMIT 1';
 $data = mysql_query("SELECT * FROM questions ORDER BY RAND() LIMIT 1") 
 or die(mysql_error()); 


 $info = mysql_fetch_array( $data ); 


 Print "<b>question:</b> ".$info['question'] . " ";


 ?>
 <form action="result.php" method="post">
 <input type="hidden" name="checkbox" value=<?php
  $info ['correctoption'] ?>>
  <?php
  Print $info ['option1'] ?>  <input type="radio" name="ans" value=<?php
  $info ['option1'] ?>  /><br />
  <?php
  Print $info ['option2'] ?> <input type="radio" name="ans" value=<?php
  $info ['option2'] ?>   /><br />
  <?php
  Print $info ['option3'] ?> <input type="radio" name="ans" value=<?php
  $info ['option3']  ?>   /><br />
  <?php
  Print $info ['option4']  ?> <input type="radio" name="ans" value=<?php $info ['option4'] ?>   /><br />
  <input type="submit" value="submit" />
</form>

 </body>
 </html>

However since the question is random I can't get figure out how to get it checked on the following page

 <?php 

 mysql_connect("localhost", "trivia", "<snip>") or die(mysql_error()); 
 mysql_select_db("trivia") or die(mysql_error()); 




 $query = 'SELECT * FROM questions ORDER BY RAND() LIMIT 1';
 $data = mysql_query("SELECT * FROM `questions` WHERE 1") 
 or die(mysql_error()); 


 // puts the "friends" info into the $info array 
 $info = mysql_fetch_array( $data ); 

 $correctoption =  $_POST ['checkbox'];


$answer = $_POST['ans'];

    if ($answer == 
  $correctoption) {

        echo 'You are Correct';


    }

    else {

        echo 'You are Incorrect';

    }    

?> 

I have been searching for an answer to this and haven't gotten anywhere.

Upvotes: 1

Views: 199

Answers (2)

Sumoanand
Sumoanand

Reputation: 8929

Retain question id in a hidden input field:

<input type="hidden" name="questionNumber" id="questionNumber" value="12345" />

Example: http://www.tizag.com/htmlT/htmlhidden.php
This way, you know the user response & question which user answered on submit.

Hope this will help.

Upvotes: 1

Robin Manoli
Robin Manoli

Reputation: 2222

Hopefully your questions have id's in the database. Use the question id as a hidden input field in your form. On the second page then, you only need to select the question which has the question id:

$query = 'SELECT * FROM questions WHERE id=' .mysql_real_escape_string($_POST['id']). ' LIMIT 1';

Also, you shouldn't post the correct answer in the html, it will be very easy to figure out for the users!

Upvotes: 0

Related Questions