1

I have two MySql tables for all of my Questionnaries as following:

Table 1:

id, quiz_name, quiz_no.

Table 2:

id, quiz_no, question, ans1, ans2, ans3, ans4, ans5.

Regarding the tables, I want to output data in separated divs with name of quiz on top and all the question and answers rows which belong of specific quiz_no (meaning to the specific quiz title) below. But all I got is all the rows with all the questions and answers below the same quiz_name at every title.

My code is:

quiz.php

$table1 = "SELECT * FROM table1";

if (!empty($_POST['num'])) {
  $no = $_POST['num'];
  $table2 = "SELECT * FROM table2 WHERE quiz_no = '".$no."'"; //Notice: Undefined variable: table2 in C:\... quiz.html
}
// So
$table2 = "SELECT * FROM table2";

quiz.html

<?php
      if($mysqli->multi_query($table1)){do{$result = $mysqli->store_result();
        while($row = $result->fetch_assoc()){
?>
        <div id="head_view_pers" class="modal-header" style="cursor:pointer;background-color:#7F4532;border:1px solid #7F4532;">
            <h4 style="font-weight:bold;color:#fff;margin-left:10px;" class="modal-title"><?php echo $row2['nume_chestionar'];?></h4>
            <i class="fas fa-expand" style="position:relative;top:6px;float:right;padding:0 10px 10px 10px;font-size:30px;font-weight:bold;color:#fff;"></i>
        </div>
        <article id="view_quiz_pers" class="view_quiz_pers" style="">
             <div style="padding: 0;" class="view_chestionare_pers">
                  <div class="div_title" style="border-bottom: 1px solid #000000;margin:10px 24px 15px 24px;padding-bottom:4px;font-size:28px;text-align:center;font-weight:bold;"><?php echo $row['quiz_name']; ?></div>

                  <form action="quiz.php" method="post">
                       <input type="hidden" id="num" class="num" value="" name="num[]"></input>
                       <div class="view_pers_wrapper" style="clear:both;text-align:justify;margin:0 40px;">
<?php                    
                         if($mysqli->multi_query($table2)){do{$result2 = $mysqli->store_result();
                              while($row2 = $result2->fetch_assoc()) {
?>
                                <div class="view_questions" style="clear:both;padding:0 0 10px 0;font-weight:bold;list-style-type:none;margin:0 24px;"><?php echo $row2['id']; ?>. <?php echo $row2['question'];?></div>
                                  <ul class="view_answers" style="clear:both;margin: 0 24px;list-style-type:none;">
                                    <li class="li_ans1" ><?php if($row['ans1']) {echo '<b>a. </b>'.$row['ans1'];} ?></li>
                                    <li class="li_ans2" ><?php if($row['ans2']) {echo '<b>b. </b>'.$row['ans2'];} ?></li>
                                    <li class="li_ans3" ><?php if($row['ans3']) {echo '<b>c. </b>'.$row['ans3'];} ?></li>
                                    <li class="li_ans4" ><?php if($row['ans4']) {echo '<b>d. </b>'.$row['ans4'];} ?></li>
                                    <li class="li_ans5" style="padding-bottom:24px;"><?php if($row['ans5']) {echo '<b>e. </b>'.$row['ans5'];} ?></li>
                                  </ul>
<?php 
                        }mysqli_free_result($result);}while($mysqli->more_results() && $mysqli->next_result());} 
?>
                                </div>
                        </div>
                  </form>
        </article>
<?php 
      }mysqli_free_result($result2);}while($mysqli->more_results() && $mysqli->next_result());} 
?>

Screenshot

EDIT:

<?php
 include 'db.php';

 $table1 = "SELECT * FROM table1";

 if (!empty($_POST['num'])) {
   $no = $_POST['num'];
   $table2 = "SELECT * FROM table2 WHERE quiz_no = '".$no."'"; //Notice: Undefined variable: table2 in C:\... quiz.html
 }

*The quizzes are inserted in the desired order. In the insertion page you can add only one questionnaire at time.

Here is the MySql database with the two tables: https://ibb.co/k624S82

3
  • Are the questions in the desired order in the database table? If not, then you might want to add a field such as question_no to specify the order and then order the results using that column. Commented Mar 3, 2019 at 12:42
  • Also, is the id of the questions an auto incrementing integer? Commented Mar 3, 2019 at 13:03
  • the questions are on the same row with the answers and each row has auto increment id Commented Mar 3, 2019 at 13:12

2 Answers 2

1

I think the clue is in the code comments. This query
$table2 = "SELECT * FROM table2 WHERE quiz_no = '".$no."'"; seems to be what you want, because it "joins" table2 with table1. Unfortunately as you have noted, it gives this error
//Notice: Undefined variable: table2 in C:\... quiz.html.

It got replaced with this $table2 = "SELECT * FROM table2"; to mitigate the error. Which it did, ie it no longer gives the error, but it causes the next problem: not giving the correct result. It is a scope problem: since $table2 is declared inside the if block, it is only available in that block. Moving the table2 select outside the if was a good start, but now it doesn't "join" the tables.

This should give you the information you need to make progress on the problem.

Sign up to request clarification or add additional context in comments.

Comments

0

You are sending the value in an array in your form name="num[]" as such I suspect that your query doesn't work, and your undefined variable come from the query not being set at all,

$no = $_POST['num'];
  $table2 = "SELECT * FROM table2 WHERE quiz_no = '".$no."'";<-- you are trying to concatenate an array as a string

and it's then overwritten anyway as mentioned by @DinoCodeSaurus

What you need to do is get the value out of your post array ($_POST[num][0]) or, what you probably mean to do is just set it to a single value anyway and chage !empty($_POST['num'] to isset($_POST['num'])

unless i'm missing something

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.