Jump to content

[SOLVED] Can this be done?


charliepoo

Recommended Posts

Okay, so I'm trying to do a random question and answer php coding using a database to store each question, answer and ID.

So far I've managed to get the random question, using this code:

<?php
//select database for use
mysql_select_db ('carlott_ravie') or die ('Unable to select database!');

//create and exectue query
$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `questions` ");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM `questions` LIMIT $offset, 1 " ); 

//check if records were returned
if (mysql_num_rows($result) > 0)
{
//print HTML
//iterate over record set
// print each field
while($row = mysql_fetch_row($result))
{	echo '' . $row[1] . '';
	echo '</div></center>';

}
}
else
{
	//print error message
echo 'No events in planning.';
}

//once processing is complete
// free result set
mysql_free_result($result);


// close connection to MySQL server
mysql_close($connection)
?>

What I'm wanting to do further than that, is have the user enter the answer and check it against the stored answer in the database. If it's right the user gets a button or redirected to another page, if it's wrong, the user gets redirected to a different page telling them it's wrong.

row[2] is where the answers are in the database, so I'm figuring since it's random, I'd have to load the question into a variable in the same php session, but my attempts have been unsuccessful.

I have no idea how to check the user's answer is the same as the answer stored. Any help would be greatly appreciated.

Link to comment
Share on other sites

first, you can use ignace's query if you add "LIMIT 1" to the end of it to restrict the set to one. second, in order to check the user's input against the answer, simply put a hidden input into the form containing the question's ID. then on submission, use that ID to summon the answer from the database and check it against the answer the user provided. from there, i presume you're able to do the rest (forwarding the user or showing a button, etc).

Link to comment
Share on other sites

Cheers, that helped alot (:

Sorry, I'm really new to this. I've managed to do what you said, to put in the hidden input and to write the query needed, but now I'm stuck on how to process it so that I can check that the answer is the same as the one in the database. I am also not sure if my calling of the variable $qid is correct.

<?php 
$answer = $_POST['answer']
$qid = $_POST['qid'];

//select database for use
mysql_select_db ('carlott_ravie') or die ('Unable to select database!');

//create and execute query
$offset_result = mysql_query( " SELECT `questions`.`Qid`, `questions`.`Answer`
FROM questions
WHERE (`questions`.`Qid` $qid) " )
        // is the last part correct?

//check if records were returned
if (mysql_num_rows($result) > 0)
{
this is the part I am having trouble with
}
//once processing is complete
// free result set
mysql_free_result($result);


// close connection to MySQL server
mysql_close($connection)
?>

 

 

Link to comment
Share on other sites

<?php 

//create and execute query
$offset_result = mysql_query( " SELECT `questions`.`Qid`, `questions`.`Answer`
FROM questions
WHERE (`questions`.`Qid` $qid) " )
        // is the last part correct?

//check if records were returned
if (mysql_num_rows($offset_result) > 0)
{
You made a typo on the variable. These mistakes are a pain in the ass
}

?>

Link to comment
Share on other sites

Cheers, that helped alot (:

Sorry, I'm really new to this. I've managed to do what you said, to put in the hidden input and to write the query needed, but now I'm stuck on how to process it so that I can check that the answer is the same as the one in the database. I am also not sure if my calling of the variable $qid is correct.

<?php 
$answer = $_POST['answer']
$qid = $_POST['qid'];

//select database for use
mysql_select_db ('carlott_ravie') or die ('Unable to select database!');

//create and execute query
$offset_result = mysql_query( " SELECT `questions`.`Qid`, `questions`.`Answer`
FROM questions
WHERE (`questions`.`Qid` $qid) " )
        // is the last part correct?

//check if records were returned
if (mysql_num_rows($result) > 0)
{
this is the part I am having trouble with
}
//once processing is complete
// free result set
mysql_free_result($result);


// close connection to MySQL server
mysql_close($connection)
?>

 

 

 

first off, you've got an error in your query. you need to add an operator to that WHERE clause:

 

SELECT `Answer`
FROM `questions`
WHERE `Qid` = '$qid')

 

i also removed some unnecessary stuff (didn't need to select Qid, and you don't need the table notation when selecting from only one table). second, you're on the right track for checking the answer against the one submitted. within that if() statement (which batosi has corrected for you), you will simply need to check if the two match:

 

if (num_rows thing)
{
  $row = mysql_fetch_assoc($offset_result);
  if ($_POST['answer'] == $row['Answer'])
  {
    // answer is correct
  }
  else
  {
    // answer is incorrect
  }
}

 

keep in mind you're at the mercy of the machine here - if they aren't identical in spelling, case, and whitespace, the computer won't technically consider them matched.

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.