0
votes

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.' &nbsp;&nbsp;<br>';
    echo '<input type="radio" name="Answer['.$qID.']" value="B"> B.  '.$b.' &nbsp;&nbsp;<br>';
    echo '<input type="radio" name="Answer['.$qID.']" value="C"> C.  '.$c.' &nbsp;&nbsp;<br>';
    echo '<input type="radio" name="Answer['.$qID.']" value="D"> D.  '.$d.' &nbsp;&nbsp;<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);
?>

enter image description here

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

2
What does your submit button look like? - BobRodes
I added a snippet of what the query results to the original comment. - Joshua Thornburg
Sorry, didn't see the code line for your submit button. That's what I was looking for. - BobRodes

2 Answers

1
votes

I see you're a new contributor so welcome to SO!

I believe that the indexes are undefined because you are not checking if they exist first, just like you do when you check for $_POST['submit'].

So I would do this at the beginning of your code:

$limitValue = 0; // or any other default value
$type = 'myTopic'; // or any other default value
if(isset($_POST['count'])){
    $limitValue = $_POST['count']; // overwrite the default value
}
if(isset($_POST['topic'])){
    $type = $_POST['topic']; // overwrite the default value
}

In any case, I don't know how you want your page to behave, but I suppose it's best to just have one single "submit" button to send it all together and print it all together.

Details

So, looking at the code, I was wrong about the nested while loop! It was just a little poorly indented so I didn't see a closing curly bracket.

Anyway, I rewrote the code and I believe that this should work:

<form name="quiz" method="POST" action="">
<?php
$limitValue = 0; // or any other default value
$type = ['myTopic']; // or any other default value
if(isset($_POST['count'])){
    $limitValue = $_POST['count']; // overwrite the default value
}
if(isset($_POST['topic'])){
    $type = $_POST['topic']; // overwrite the default value
}
$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 < count($_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 = $link->query($sql);
if($result->num_rows > 0){
    // output data from each row
    echo '<input type="hidden" name="count" value="'.$_POST['count'].'">';
    echo '<input type="hidden" name="topic" value="'.$_POST['topic'].'">';
    while($row = $result->fetch_object()){
        $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->questionType.'<br><br>' .
        'Question: '.$row->question.'<br><br><br>';
        echo '<input type="radio" name="Answer['.$qID.']" value="A"> A.  '.$a.' &nbsp;&nbsp;<br>';
        echo '<input type="radio" name="Answer['.$qID.']" value="B"> B.  '.$b.' &nbsp;&nbsp;<br>';
        echo '<input type="radio" name="Answer['.$qID.']" value="C"> C.  '.$c.' &nbsp;&nbsp;<br>';
        echo '<input type="radio" name="Answer['.$qID.']" value="D"> D.  '.$d.' &nbsp;&nbsp;<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">';
}
$link->close();
?>
</form>
0
votes

You could try saving it as a temparity cookie and/or store it to localstorage.