I'm running this code. It displays a question and possible answers. When I click the submit button. It goes to a blank page.
I get errors "undefined index count" and "undefined index topic". So this tells me that it's running the query again but without the value of the indexes set.
How do I get this submit button to just display the data column "rightAnswer" for the data selected by the query instead of just running the query again?
<form name="quiz" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="count" value="<?php echo $_POST['count'] ?>">
<input type="hidden" name="topic" value="<?php echo $_POST['topic'] ?>">
<?php
define('DB_NAME', 'quizquestions');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
# connects to the database
$link = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD);
# Provides feedback that the connection is made
if (!$link) {
die ('could not connect:' . mysqli_error());
}
#connects tot he database
$db_selected = mysqli_select_db($link, DB_NAME);
if (!$db_selected) {
die('Can\'t use') . DB_NAME;
}
$limitValue = 0;
$type = 'myTopic';
if(isset($_POST['count'])){
$limitValue = $_POST['count'];
}
if(isset($_POST['topic'])){
$type = $_POST['topic'];
}
$qType = join("','", $type);
if(isset($_POST['submit'])){
// create a list of SQL "OR"s to select only those questions that were answered rather than randomly.
$questionList = "`question_id`=".$_POST['Question'][0];
for($i = 1; $i < $_POST['Question']; $i++){
$questionList .= " OR `question_id`=".$_POST['Question'][$i];
}
$sql = "SELECT `question_id`, `questionType`, `question`, `answerA`, `answerB`, `answerC`, `answerD`, `rightAnswer` FROM `questions` WHERE $questionList";
} else {
// randomly select as many questions as specified
$sql = "SELECT `question_id`, `questionType`, `question`, `answerA`, `answerB`, `answerC`, `answerD`, `rightAnswer` FROM `questions` WHERE `questionType` IN ('$qType') ORDER BY RAND() LIMIT $limitValue";
}
$result = mysqli_query($link, $sql);
$num = mysqli_num_rows($result);
// output data from each row
while ($row = mysqli_fetch_array($result)) {
$qID = $row->question_id;
$a = $row->answerA;
$b = $row->answerB;
$c = $row->answerC;
$d = $row->answerD;
$rA = $row->rightAnswer;
echo 'QID: '.$qID.'<br>'.
'Question Type: '.$row->question_type.'<br><br>' .
'Question: '.$row->question.'<br><br><br>';
echo '<input type="radio" name="Answer['.$qID.']" value="A"> A. '.$a.' <br>';
echo '<input type="radio" name="Answer['.$qID.']" value="B"> B. '.$b.' <br>';
echo '<input type="radio" name="Answer['.$qID.']" value="C"> C. '.$c.' <br>';
echo '<input type="radio" name="Answer['.$qID.']" value="D"> D. '.$d.' <br><br>';
echo '<input type="hidden" name="Question[]" value="'.$qID.'">';
if(isset($_POST['submit'])){
if(isset($_POST['Answer'][$qID])){
if($_POST['Answer'][$qID] == $rA){
echo '<p style="color: darkseagreen;">Correct!</p>';
} else {
echo '<p style="color: indianred;">Wrong!</p>';
}
echo 'Right Answer: '.$rA.'<br><hr>';
} else {
echo '<p style="color: darkorange;">No answer!</p>';
}
echo '<br><br><hr>';
}
}
echo '<input name="submit" type="submit" value="Submit">';
mysqli_free_result($result);
mysqli_close($link);
?>
Adding most recent entries from error_log:
[26-Aug-2018 21:25:43 UTC] PHP Notice: Undefined index: count in /home/myd2n7jhklvu/public_html/quiz.php on line 2
[26-Aug-2018 21:25:43 UTC] PHP Notice: Undefined index: topic in /home/myd2n7jhklvu/public_html/quiz.php on line 3
[26-Aug-2018 21:25:43 UTC] PHP Warning: join(): Invalid arguments passed in /home/myd2n7jhklvu/public_html/quiz.php on line 30
[26-Aug-2018 21:25:46 UTC] PHP Warning: join(): Invalid arguments passed in /home/myd2n7jhklvu/public_html/quiz.php on line 30
[26-Aug-2018 21:25:46 UTC] PHP Notice: Undefined variable: qID in /home/myd2n7jhklvu/public_html/quiz.php on line 54
[26-Aug-2018 21:25:47 UTC] PHP Notice: Array to string conversion in /home/myd2n7jhklvu/public_html/quiz.php on line 3
[26-Aug-2018 21:26:04 UTC] PHP Warning: join(): Invalid arguments passed in /home/myd2n7jhklvu/public_html/quiz.php on line 30
[26-Aug-2018 21:26:04 UTC] PHP Notice: Undefined variable: qID in /home/myd2n7jhklvu/public_html/quiz.php on line 54
