如何根据用户的分支显示考试页面?

时间:2013-08-18 11:39:40

标签: php html

我想根据学生的分支显示问题文件。 我在数据库“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>

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

需要小改动

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