SQL Inner Join每次都会获取不同的结果

时间:2017-08-05 16:16:57

标签: php mysql

我的项目堆栈在这个问题中如下,

我有2个表 quiziz (由老师填写)有10个小册子

id,dept,subject,question,ans1,ans2,ans3,ans4,correct_ans,techername

学生有7个小册子

ID,用户名,密码,用户类型,部门,主题,答案

如果用户在 dborad.php 中成功登录,他会看到每个链接的链接都会将他带到 quiz.php 页面中的不同测验(使用get方法获取学生的主题和部门),在这个 quiz.php 页面中,他将看到他的测验有4个答案,我的问题是内连接不起作用,它假设获取他从 dborad.php 中选择的特定主题的测验,其中一些问题与其他学生不同,但不是

dboard.php

if (isset($_SESSION['username'])) {
if ($_SESSION['usertype'] === "isstudent") {
$SQLgetParea = "SELECT * From students WHERE username = '$_SESSION[username]' ";
          $SQLgetPareaResult = mysqli_query($conn,$SQLgetParea);
          $SQLgetPareaRow = mysqli_fetch_array($SQLgetPareaResult,MYSQLI_NUM);
          $getDept = $SQLgetPareaRow[4];

            $sql2 = "SELECT students.*, quiziz.*
                      FROM students 
                      INNER JOIN quiziz 
                      ON students.dept=quiziz.dept 
                      AND students.subject = quiziz.subject 
                      WHERE students.dept = '".$getDept."' 
                        ";
                $result2 = mysqli_query($conn,$sql2);

                while ($row2 = mysqli_fetch_array($result2,MYSQLI_NUM)) {
                  echo "
          <a href='quiz.php?subject=".$row2[5]."?dept=".$row2[4]."'>Enter Exam for ".$row2[5]."</a>
          <br><br>";
        }
}
}

quiz.php

if(isset($_SESSION['username'])){
              if ($_SESSION['usertype'] === "isstudent") {

                $conn->query("SET NAMES utf8"); 
                $conn->query("SET CHARACTER SET utf8");

                $SQLgetParea = "SELECT * From students ";
                $SQLgetPareaResult = mysqli_query($conn,$SQLgetParea);
                $SQLgetPareaRow = mysqli_fetch_array($SQLgetPareaResult,MYSQLI_ASSOC);
                $getSub = $SQLgetPareaRow['subject'];

               $sql2 = "SELECT quiziz.*, students.*
                        FROM quiziz 
                        INNER JOIN students 
                        ON quiziz.dept=students.dept 
                        AND quiziz.subject = '".$getSub."' ";
                $result2 = mysqli_query($conn,$sql2);

                while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {

                  echo "
                  <li>".$row2['question']."</li>
                  <li><div class='ruler'></div></li>
                  <li>".$row2['ans1']."</li>
                  <li>".$row2['ans2']."</li>
                  <li>".$row2['ans3']."</li>
                  <li>".$row2['ans4']."</li>
                  ";
                }

              }
            }

1 个答案:

答案 0 :(得分:0)

要为每个用户提取不同的问题,请创建一个问题表,并为要向用户展示的每个问题指定一个ID。

之后,您只需要创建一个带有随机函数的向量,以便从数据库中通过id获取这些问题。将列问题从quizes更改为question_id,这是您最近创建的表的外键。