/    Sign up×
Community /Pin to ProfileBookmark

Multiple Choice-True False Quiz

Hi guys hope your all well,

I want to put a quiz into my website that I’m doing. I’ve found some code for a Multiple Choice-True False Quiz

Here is the code, (yes I know its long and there are some reputation in it) You have to add questions by using a form to add either

Multiple Choice
or True False questions this is why there is so much code

When I try and add a question via the true/false button I get the following error

Notice: Undefined index: answer3 in C:xampphtdocsquizaddQuestions.php on line 16

Notice: Undefined index: answer4 in C:xampphtdocsquizaddQuestions.php on line 17
No database selected

Any help or advice would be much appreciated

Rich

[B][U]ONLY THING IS GUYS MY CODE IS TOO LONG TO POST [/U][/B]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmMay 22.2014 — it means you have a bad index in use with an array variable, or more likely in your situation, you are trying to find an input element in the $_POST array that doesn't exist. When retrieving inputs you must check if the var is set and then begin using it. For ex.:

[code=php]
if (!isset($_POST['answer3']))
$errmsg .= "you must answer question #3";
else
{
$answer3 = $_POST['answer3'];
if (($answer3 !== true) || ($answer3 !== false))
$errmsg .= "Invalid answer to question #3";
}
[/code]


PS - we actually prefer when people only post the code needed to document their trouble and to pinpoint where they need help. Glad you didn't post it all, but you could have posted the error lines.
Copy linkTweet thisAlerts:
@stokie-ruchauthorMay 23.2014 — Sorry I forgot all about to post the code

Here is the code that I should've posted, in my mind I'm thinking of simplifying the code by having php code for true/false and multichoice code in separate files just for simplicity

[code=php]<?php

if(isset($_POST['desc'])){
if(!isset($_POST['iscorrect']) || $_POST['iscorrect'] == ""){
echo "Sorry, important data to submit your question is missing. Please press back in your browser and try again and make sure you select a correct answer for the question.";
exit();
}
if(!isset($_POST['type']) || $_POST['type'] == ""){
echo "Sorry, there was an error parsing the form. Please press back in your browser and try again";
exit();
}
require_once("quiz/config.php");
$question = $_POST['desc'];

If (isset($_POST['answer3'])) {$answer3 = $POST['answer3'];}

$answer1 = $_POST['answer1'];

$answer2 = $_POST['answer2'];

$answer3 = $_POST['answer3'];

$answer4 = $_POST['answer4'];

$type = $_POST['type'];

$type = preg_replace('/[^a-z]/', "", $type);

$isCorrect = preg_replace('/[^0-9a-z]/', "", $_POST['iscorrect']);

$answer1 = strip_tags($answer1);

$answer1 = mysql_real_escape_string($answer1);

$answer2 = strip_tags($answer2);

$answer2 = mysql_real_escape_string($answer2);

$answer3 = strip_tags($answer3);

$answer3 = mysql_real_escape_string($answer3);

$answer4 = strip_tags($answer4);

$answer4 = mysql_real_escape_string($answer4);

$question = strip_tags($question);

$question = mysql_real_escape_string($question);

//<input name="answer[IDENTIFIER]" value="yourvalue" />

if($type == 'tf'){
if((!$question) || (!$answer1) || (!$answer2) || (!$isCorrect)){
echo "Sorry, All fields must be filled in to add a new question to the quiz. Please press back in your browser and try again.";
exit();
}
}
if($type == 'mc'){
if((!$question) || (!$answer1) || (!$answer2) || (!$answer3) || (!$answer4) || (!$isCorrect)){
echo "Sorry, All fields must be filled in to add a new question to the quiz. Please press back in your browser and try again.";
exit();
}
}
$sql = mysql_query("INSERT INTO questions (question, type) VALUES ('$question', '$type')")or die(mysql_error());
$lastId = mysql_insert_id();
mysql_query("UPDATE questions SET question_id='$lastId' WHERE id='$lastId' LIMIT 1")or die(mysql_error());
//// Update answers based on which is correct //////////
if($type == 'tf'){
if($isCorrect == "answer1"){
$sql2 = mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '1')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '0')")or die(mysql_error());
$msg = 'Thanks, your question has been added';
header('location: addQuestions.php?msg='.$msg.'');
exit();
}
if($isCorrect == "answer2"){
$sql2 = mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '1')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '0')")or die(mysql_error());
$msg = 'Thanks, your question has been added';
header('location: addQuestions.php?msg='.$msg.'');
exit();
}
}
if($type == 'mc'){
if($isCorrect == "answer1"){
$sql2 = mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '1')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '0')")or die(mysql_error());
$msg = 'Thanks, your question has been added';
header('location: addQuestions.php?msg='.$msg.'');
exit();
}
if($isCorrect == "answer2"){
$sql2 = mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '1')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '0')")or die(mysql_error());
$msg = 'Thanks, your question has been added';
header('location: addQuestions.php?msg='.$msg.'');
exit();
}
if($isCorrect == "answer3"){
$sql2 = mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '1')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '0')")or die(mysql_error());
$msg = 'Thanks, your question has been added';
header('location: addQuestions.php?msg='.$msg.'');
exit();
}
if($isCorrect == "answer4"){
$sql2 = mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '1')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '0')")or die(mysql_error());
$msg = 'Thanks, your question has been added';
header('location: addQuestions.php?msg='.$msg.'');
exit();
}
}
}
?>
<?php [/code]
Copy linkTweet thisAlerts:
@ginerjmMay 23.2014 — Great - you posted the code. BUT - did you take my suggestion and look thru the code first to see if I had the answer?
Copy linkTweet thisAlerts:
@stokie-ruchauthorMay 23.2014 — sorry i didnt i will have a look through it now and get back to you
×

Success!

Help @stokie-ruch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...