Jump to content

multiple choice quiz


ke7mzp

Recommended Posts

created a table called "questions" like this:

id -- question

 

created a table called "choices" like this:

id -- question_id -- text -- correct

 

set all the correct answers to "1" in the correct column.

<?php
$result = mysql_query("SELECT * FROM `questions` ORDER BY RAND() LIMIT 30");

while($row = mysql_fetch_array($result)){
$q_id =  $row['question_id'];
//display question here


$choices = mysql_query("SELECT * FROM `choices` WHERE `question_id`='$q_id' ORDER BY RAND() ");
while($row2 = mysql_fetch_array($choices){
//display answers here
}
}
?>

 

and thats pretty much the jist of displaying the test

 

Link to comment
https://forums.phpfreaks.com/topic/110520-multiple-choice-quiz/#findComment-566994
Share on other sites

No one here is going to create this kind of thing for you. What you are asking for is a pretty standard input form, db storage, extraction layer and display method.

 

That may sound complicated, but what your asking is some of the most common pieces of PHP. I would suggest you start learning PHP & MySql by reading a book, there are TONS of recommendations on here if you look for them. Find the member Thorpe, he has a beginning PHP link in his sig.

 

Or...... you can look for a pre-coded script to use.

 

Nate

Link to comment
https://forums.phpfreaks.com/topic/110520-multiple-choice-quiz/#findComment-567495
Share on other sites

I have played around with this code and when I try to add a question to the database it gives me an error

"Could not connect:" yet I Know that the server is working and the login information is accurate. Can someone help me

 

Hear is the code for the form that I am trying to submit

 

<html>

<body>

<form action="insert.php" method="post">

Q_ID: <input type="text" name="ID" />

<br />

Q_ID2: <input type="text" name="question_ID" />

<br />

Question: <input type="text" name="question" />

<br />

<input type="checkbox" name="Cans1" value="1"  />

ANS1: <input type="text" name="ans1" />

<br />

<input type="checkbox" name="Cans2" value="1"  />

ANS2: <input type="text" name="ans2" />

<br />

<input type="checkbox" name="Cans3" value="1"  />

ANS3: <input type="text" name="ans3" />

<br />

<input type="checkbox" name="Cans4" value="1"  />

ANS4: <input type="text" name="ans4" />

<br />

<input type="submit" value="submit" />

</form>

 

</body>

</html>

 

Hear is the code for the insert.php page

 

<?php

$con = mysql_connect(localhost,root,********);

if ($con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("demo", $con);

 

$sql="INSERT INTO question_id (ID, question)

VALUES

('$_POST[question_ID]','$_POST[Cans1]','$_POST[ans1]','$_POST[Cans2]','$_POST[ans2]','$_POST[Cans3]','$_POST[ans3]','$_POST[Cans4]'),'$_POST[ans4]'";

 

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "record added";

mysql_close($con)

?>

<?php

$con = mysql_connect(localhost,root,qwertyztp99);

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("demo", $con);

 

$sql="INSERT INTO question (ID, question)

VALUES

('$_POST[iD]','$_POST[question]'";

 

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "record added";

 

mysql_close($con)

?>

 

Link to comment
https://forums.phpfreaks.com/topic/110520-multiple-choice-quiz/#findComment-567882
Share on other sites

$sql="INSERT INTO question_id (ID, question)
VALUES
('$_POST[question_ID]','$_POST[Cans1]','$_POST[ans1]','$_POST[Cans2]','$_POST[ans2]','$_POST[Cans3]','$_POST[ans3]','$_POST[C

 

You put in too many values. It should be something like,

 

$sql="INSERT INTO question_id (ID, question, answer)
VALUES
('$_POST[question_ID]','$_POST[Cans1]','$_POST[ans1]')";

 

By the way, you'll need to add an answer column in question_id table.

 

Link to comment
https://forums.phpfreaks.com/topic/110520-multiple-choice-quiz/#findComment-567896
Share on other sites

I Fixed that error but now the page I call it from keeps giving me errors

Like

" Parse error: syntax error, unexpected '{' in D:\server\MyApache\htdocs\New Folder\New Folder\testapp.php on line 18"

 

Hear is the code for that

<?php
$con = mysql_connect(localhost,root,qwertyztp99);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("demo", $con);

$result = mysql_query("SELECT * FROM `questions` ORDER BY RAND() LIMIT 30");

while($row = mysql_fetch_array($result)){
$q_id =  $row['question_id'];
//display question here


$choices = mysql_query("SELECT * FROM `question_id` WHERE `question_id`='$q_id' ORDER BY RAND() ");
while($row2 = mysql_fetch_array($choices){
//display answers here
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/110520-multiple-choice-quiz/#findComment-567930
Share on other sites

I don't know how order by rand() would work, so try this.

 

<?php
$con = mysql_connect(localhost,root,qwertyztp99);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("demo", $con);

$result = mysql_query("SELECT * FROM questions LIMIT 30");

while($row = mysql_fetch_array($result)){
$q_id =  $row['question_id'];
//display question here


$choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id'");
while($row2 = mysql_fetch_array($choices){
//display answers here
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/110520-multiple-choice-quiz/#findComment-567934
Share on other sites

<?php
$con = mysql_connect(localhost,root,qwertyztp99);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("demo", $con);

$result = mysql_query("SELECT * FROM questions LIMIT 30");

while($row = mysql_fetch_array($result)){
$q_id =  $row['question_id'];
//display question here


$choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id'");
while($row2 = mysql_fetch_array($choices)) {
//display answers here
}
}
?>

Okay try that D: You forgot a ) after the while function. I missed it xD

Link to comment
https://forums.phpfreaks.com/topic/110520-multiple-choice-quiz/#findComment-567945
Share on other sites

I had it working till i added this code:

FROM question_id WHERE question_id='$q_id"

 

to this line

 

$choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id" );

 

Hear is the code for the page that is trying to display the database

<?php
$con = mysql_connect("localhost","root","qwertyztp99");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("demo", $con);
{
$result = mysql_query("SELECT * FROM question ORDER BY RAND() LIMIT 1");

while($row = mysql_fetch_array($result))
  {
$q_id =  $row['question_id'];
  echo $row['ID'] . " " . $row['question'];
  echo "<br />";
  }
}
mysql_close($con);
?>
<?php
$con = mysql_connect(localhost,root,qwertyztp99);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("demo", $con);
$choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id" );

while($row2 = mysql_fetch_array($choices))
  {
echo "<br />";
echo $row2['ans_a']	;
echo "<br />";
echo $row['ans_b'];
echo "<br />";
echo $row['ans_c'];
echo "<br />";
echo $row['ans_d'];

//display answers here
}
?>

 

Hear is what this displays

 

2 What letters must be used for the first letter in US amateur call signs?

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\server\MyApache\htdocs\New Folder\New Folder\sqltest\table2.php on line 31

 

Link to comment
https://forums.phpfreaks.com/topic/110520-multiple-choice-quiz/#findComment-568340
Share on other sites

if you are just learning php now, you need to do some simplier things first.....

 

this:

$choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id" );

 

must be this:

$choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id' " );\

 

you forgot to end the single quotes

Link to comment
https://forums.phpfreaks.com/topic/110520-multiple-choice-quiz/#findComment-568456
Share on other sites

$choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id" );

 

So you have a table named question_id with a column named question_id?

 

typically a table and column don't have the same name.

 

This is the proper structure for this type of query.

 

"SELECT * FROM tablename WHERE column = 'argument'"

 

In your query, I just noticed that you forgot a ' after $q_id

 

ok new rule for you...

 

ALWAYS add

or die(mysql_error());

to every single query or db select

 

mysql_query("you query") or die(mysql_error());

 

I think that should be a rule for almost every coder. Especially newbies. I still use this line faithfully as it can help you track down an error very quickly.

 

 

Link to comment
https://forums.phpfreaks.com/topic/110520-multiple-choice-quiz/#findComment-568504
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.