0

我想根据学生的分支显示试卷。我在数据库“aptitude”中创建了 5 个表。表格如下:itque、etrxque、extcque、civilque、mechque。

分支名称存储在“注册”数据库中。

如果学生在 IT 部门,则只应显示 IT 的试卷。

但是逻辑上有一些问题。试卷不按学生所在学科展示。

test.php 的代码:

<html>
    <body>
<?php
      require_once('appvars.php');
      require_once('connectvars.php');

      // Make sure the user is logged in before going any further.
      if (!isset($_SESSION['user_id'])) {
        echo '<span style="color:#FFF;text-align:center;"><p class="login">Please <a href="login.php"style="color: #FFFFFF">log in</a> to access this page.</p></span>';
        exit();
      }
      else {
        include("menu.php");
      }
      ?>
      <?php
     $dbc=mysqli_connect("localhost","root","","register");
        $query = "SELECT branch FROM user WHERE m_id = '" . $_SESSION['user_id'] . "'";
        $data = mysqli_query($dbc, $query);
        $row = mysqli_fetch_array($data);
       if ($row != NULL) {
          $Branch = $row['branch'];

        }
        else {
          echo '<p class="error">There was a problem .</p>';
        }



      ?>
      <form action="./result.php" method="post">

    <?php

    $connect = mysql_connect("localhost" ,"root","");

    mysql_select_db("aptitude");



    $query = mysql_query("SELECT * FROM `$Branch` ORDER BY RAND() LIMIT 10 ");




     while($rows = mysql_fetch_array($query)):

        $q = $rows['Q_no'];
        $qus = $rows['Question'];
        $a = $rows['answer1'];
        $b = $rows['answer2'];
        $c = $rows['answer3'];
        $d = $rows['answer4'];
        $ans = $rows['correct'];
        echo" <p>&nbsp;</p>";
        echo "<p>Q:-&nbsp;$qus <br></p>";
        echo "<p>&nbsp;&nbsp;&nbsp;&nbsp;A <input type=radio name = 'answer[$q]' value = '$a'></input>$a &nbsp &nbsp<br>
    "; 
        echo "&nbsp;&nbsp;&nbsp;&nbsp;B <input type=radio name = 'answer[$q]' value = '$b'></input>$b &nbsp &nbsp<br>
    "; 
        echo "&nbsp;&nbsp;&nbsp;&nbsp;C <input type=radio name = 'answer[$q]' value = '$c'></input>$c &nbsp &nbsp<br>
     "; 
        echo "&nbsp;&nbsp;&nbsp;&nbsp;D <input type=radio name = 'answer[$q]' value = '$d'></input>$d <br><br> </p>";


        endwhile;
    ?>

    <center><input name="cmdSubmit" type="submit" id="cmdSubmit" value="Submit"/>
    </center>
    </form>
    </body>
    </html>
4

2 回答 2

0

最大的逻辑缺陷是,如果 user_id 不在会话中并且 user_id 不在 cookie 中,那么您仍然允许继续处理。您应该强制用户再次登录。

Apache 的默认会话时间为 20 分钟,因此您可能希望增加时间以允许学生完成评估,否则当他们来保存答案时,服务器将超时并且没有数据保存。

于 2013-08-18T12:17:34.787 回答
0

需要小的改动

$query = mysql_query("SELECT * FROM ".$branch." ORDER BY RAND() LIMIT 10 ");
于 2013-08-19T04:48:04.747 回答