Jump to content

Php/mysql Quiz Coding Help


teenwolf

Recommended Posts

Hey guys,

 

basically I am trying to make an online php/MySQL quiz, where the questions and answers are stored on a database.

I am having trouble getting my code to work and display the questions in radio button format, and dont know what is wrong or missing from what I have so far.

 

Here is the code I have done so far

 

Thanks

 

 

 

 

<?php

session_start();

 

require("function.php");

require("dblink.php");

$conn = "site";

$id = $_GET['id'];

$sql = "SELECT * FROM 'questions' where id =".$id;

$_SESSION['answer'] = $row['correct'];

 

 

 

//Start with question ID

generateheader("Question: ".$id=2);

//Is there a vairable in the url

if (isset($variable))

{

 

 

//get question id from user and store

$id = '2';

}

else

{

 

//no question id submitted

 

$id = '2';

 

//unset session data

unset($_SESSION['questions']);

unset($_SESSION['total']);

 

}

 

//If the questions array has been built session 'questions'

if (isset($SESSION))

{

//has the user selected a value (request)

if (isset($REQUEST))

{

//check for a correct answer, (look at stored session data later)

if ($SESSION)

{

//correct answer

$_SESSION['total'] = '';

 

}

}

else

{

//No value inputted from the user

 

}

}

//Questions array is not been built

else

{

//get a list of question id's

$sql = "SELECT id FROM questions ORDER BY id ASC";

$results = mysql_query($sql,$dblink);

 

$stringtemp = "";

$maxtemp = 1;

 

while (mysql_fetch_array($results)) //More results

{

 

$stringtemp = $stringtemp." ".$row['id'];

$maxtemp++;

}

 

//Create question id array pulled from database

$_SESSION['questions'] = explode(" ",$stringtemp);

 

//Max value of the array

$_SESSION['max']=$maxtemp;

 

}

 

 

//Return a database result to pick data from

$sql = "select * from questions where id =".$_SESSION['questions'][$id];

$result = mysql_query($sql,$dblink) ;;

$row = mysql_fetch_array($results);

 

//Store correct answer for next pageload

$_SESSION['answer'] = $row['correct'];

 

 

$nextid=$id+1;

 

//Check to see if the current page is the final question

if ($nextid == $_SESSION['max'])

{

//Final question

 

 

}

else

{

//Not the final question

 

}

 

generateheader("Question: ".$id);

 

echo "<form action=\"".$next."\" method=\"post\" name=\"form\" >

 

<p>".$row['qn']."</p>

<input type=\"radio\" name=\"value\" value=\"1\">".$row['answer1']."<br>

<input type=\"radio\" name=\"value\" value=\"2\">".$row['answer2']."<br>

<input type=\"radio\" name=\"value\" value=\"3\">".$row['answer3']."<br>

<input type=\"radio\" name=\"value\" value=\"4\">".$row['answer4']."<br>

<input class=\"submit\" type=\"submit\" value=\"Submit\"/>

</form>

</body>

</html>

";

 

?>

Link to comment
Share on other sites

Some quick comments, due to the number of issues:

  • You are vulnerable for SQL injections via your $id variable. Use intval () around $_GET['id'] when setting it, to force it to be an integer (and not an unmodified string).
  • $variable isn't being set anywhere (that I can see), thus it will always return false when checked with isset ().
  • I presume $SESSION should be $_SESSION, or rather $_SESSION['questions']. The session superglobal will always be set after you've started a session, even if it's empty.
  • Same with $REQUEST, should be $_REQUEST.
  • You're not handling errors in your SQL queries, use something like this:
    $sql = 'SELECT $fields FROM $table $CONDITIONS';
    if (!$res = mysql_query ($SQL)) {
       // Query errored out, show error.
       trigger_error (mysql_error ()."\nQuery: $SQL", E_USER_ERROR);
    }


  • You should also be using the MySQLi library, or PDO, as the old MySQL library is outdated and is going to be deprecated soon.
  • You're not saving the result from mysql_fetch_array () into the $row variable.
  • No point in creating $stringtemp with the IDs separated by a space, only to explode it into an array later. Use $_SESSION['questions'][] = $row['id'] instead, inside the while loop.
  • Your check for the final question isn't checking if the current page is the final question, but the next page.
  • The $next variable isn't set, yet.
  • Use htmlspecialchars () around content you're echoing out to HTML, which shouldn't contain HTML code. Especially user-submitted code!

Except from this, and the unfinished code, it seems like you're basically on the right way. Some things could probably have been made a bit simpler, but that's something you can focus upon once you get the code fully working. It takes experience to figure that stuff out, after all. ;)

 

However, I think your problem stems from the lack of saving the row return from the mysql_fetch_array () call, where you build up the array of the question IDs.

 

Also, please use the [code][/code] tags around your code, as it helps make both your post and your code a lot easier to read.

Link to comment
Share on other sites

Hey thanks so much for the reply!!!

 

Whenever I try to do the things mentioned I just get syntax errors throughout my code, I must be putting in them wrong.

 

are you able to paste the code with your changes to this thread? My heads about to implode with all this coding haha

Link to comment
Share on other sites

First of all, you're most welcome.

 

are you able to paste the code with your changes to this thread?

Unfortunately, I'm not. That would be me doing your job, and mine, for free. However, if you post your code her, along with the error messages, then I'm sure we can help you figure it out. I do recommend that you do a search for the error messages first though, as I can almost guarantee you that they've all been answered countless times before. Searching will help save both you and us from wasting time. ;)

 

White_Lily: Better to edit the original php.ini. I don't think that method is supported on any hosts/web servers, or if it is that number is very small. It does not work with my local Apache install, at least.

Plus the fact that he's seeing syntax errors tells me that he already has error reporting turned on.

Edited by Christian F.
Link to comment
Share on other sites

So Ive made the changed listed I think all of them hopefully...with no syntax errors this time around

 

When i preview the page I receive the error listed below relating the to the new SQL queries.

 

Should I be able to see the data in my radio buttons kept in the database at the moment? because Im still not getting anything? would this be because Im not connecting or accessing the database properly yet?

 

again thanks heaps for the help guys

 

 

"Fatal error: Query was empty Query: in /Applications/XAMPP/xamppfiles/htdocs/SITE/index.php on line 63"

 

session_start();




require("function.php");
require("dblink.php");
$conn = "site";
$id = ($_GET['id']);
$sql = "SELECT * FROM 'questions' where id =".$id;
$_SESSION['answer'] = $row['correct'];












//Start with question ID
generateheader("Question: ".$id=2);
//Is there a vairable in the url
if (isset($variable))
{








//get question id from user and store
$id = '2';
}
else
{




//no question id submitted




$id = '2';




//unset session data
unset($_SESSION['questions']);
unset($_SESSION['total']);




}




//If the questions array has been built session 'questions'
if (isset($_SESSION['questions']))
{
//has the user selected a value (request)
if (isset($_REQUEST))
{
//check for a correct answer, (look at stored session data later)
if ($_SESSION)
{
//correct answer
$_SESSION['total'] = '';




}
}
else
{
//No value inputted from the user




}
}
//Questions array is not been built
else
{
//get a list of question id's
$sql = 'SELECT $fields FROM $table $CONDITIONS';
if (!$res = mysql_query ($SQL)) {
// Query errored out, show error.
trigger_error (mysql_error ()."\nQuery: $SQL", E_USER_ERROR);
}
$results = mysql_query($sql,$dblink);




$stringtemp ="";
$maxtemp = 1;




while (mysql_fetch_array($results)) //More results
{




$stringtemp=$_SESSION['questions']=$row['id'];
$maxtemp++;
}




//Create question id array pulled from database
$_SESSION['questions']=explode(" ",$stringtemp);




//Max value of the array
$_SESSION['max']=$maxtemp;




}








//Return a database result to pick data from
$sql = 'SELECT $fields FROM $table $CONDITIONS';
if (!$res = mysql_query ($SQL)) {
// Query errored out, show error.
trigger_error (mysql_error ()."\nQuery: $SQL", E_USER_ERROR);
}
$info = mysql_fetch_array($data);
$result = mysql_query($sql,$dblink);
$row = mysql_fetch_array($results);




//Store correct answer for next pageload
$_SESSION['answer'] = $row['correct'];








$nextid=$id+1;




//Check to see if the current page is the final question
if ($nextid == $_SESSION['max'])
{
//Final question








}
else
{
//Not the final question




}




generateheader("Question: ".$id);




echo "		





".$row['qn']."		
	".$row['answer1']."

	".$row['answer2']."

	".$row['answer3']."

	".$row['answer4']."





";




?>

Edited by teenwolf
Link to comment
Share on other sites

$sql = 'SELECT $fields FROM $table $CONDITIONS';
if (!$res = mysql_query ($SQL)) {
// Query errored out, show error.
trigger_error (mysql_error ()."\nQuery: $SQL", E_USER_ERROR);

 

Did you look at the lines? PHP is case sensitive.

Link to comment
Share on other sites

is this not the radio set of buttons here at the end of my code? for some reasons it appears different in the code up a few posts.

 

The radio buttons appear when I preview the page as well as the submit button, but none of the information from the database transfers over to them.

 

 

 

 

generateheader("Question: ".$id);

 

echo "<form action=\"".$next."\" method=\"post\" name=\"form\" >

 

<p>".$row['qn']."</p>

<input type=\"radio\" name=\"value\" value=\"1\">".$row['answer1']."<br>

<input type=\"radio\" name=\"value\" value=\"2\">".$row['answer2']."<br>

<input type=\"radio\" name=\"value\" value=\"3\">".$row['answer3']."<br>

<input type=\"radio\" name=\"value\" value=\"4\">".$row['answer4']."<br>

<input class=\"submit\" type=\"submit\" value=\"Submit\"/>

</form>

</body>

</html>

";

 

?>

Link to comment
Share on other sites

Use code tags dammit.

 

In your other code that doesn't have this stuff you just posted, you have another problem.

$result = mysql_query($sql,$dblink);
$row = mysql_fetch_array($results);

 

This is basic debugging, and you should be getting undefined variable errors. 

Link to comment
Share on other sites

hey guys...

 

So I have been able to retrieve the information from the database into the radio buttons.

 

How am I able to rotate through the 6 questions I have on my database without having to make a separate page for each question?

So essentially when the user hits submit the page is refreshed with a new questions while the result is still stored in order to give the user a total score at the end of the quiz??

 

Thanks

Link to comment
Share on other sites

Insert into the database the first how many or so questions. Then load something like page.php?set=2 or something for the next questions. For the final score, just select all of the answers out of the database and display the amount correct

Edited by ExtremeGaming
Link to comment
Share on other sites

hmm.. i cant seem to get this to work

 

is this the URL you are talking about?

whenever I change the id number to another numbers question it still just brings up the first question in my database

 

 

 

//Start with question ID

generateheader("Question: ".$id=2);

//Is there a vairable in the url

if (isset($variable))

{

Link to comment
Share on other sites

Use intval () to ensure that the ID is an integer, and not some SQL code. Also, I suspect that the variable should be retrieved from the URL, which means the $_GET superglobal. Thus the code becomes:

if (isset ($_GET['variable'])) {
   $id = intval ($_GET['variable']);
} else {
   $id = 2;
   unset ($_SESSION['questions']);
   unset ($_SESSION['total']);
}

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.