使用PHP,MySQL和HTML在线测验网站

时间:2015-07-07 05:47:02

标签: php html mysql

我正在设计一个在线测验系统的网站。我已经创建了数据库和几个用于登录,添加问题和所有内容的网页。

现在我遇到以下代码中的问题。在这个测验页面中,我有一个提交按钮和下一个问题的一个链接。

submit: to store the user option in database
next: to fetch the next question from database

现在我希望提交按钮的功能是通过下一个链接完成的,当我点击下一个链接时,用户答案必须保存在数据库中,下一个问题必须来自数据库。我怎样才能做到这一点?

该页面的代码是:

{<?php @session_start(); ?>
<?php require_once('Connections/localhost.php'); ?>
<?php
if (!isset($_SESSION)) {
 session_start();
}
$MM_authorizedUsers = "3,1";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
// For security, start by assuming the visitor is NOT authorized. 
$isValid = False; 

// When a visitor has logged into this site, the Session variable      
  MM_Username set equal to their username. 
// Therefore, we know that a user is NOT logged in if that Session variable     
is blank. 
 if (!empty($UserName)) { 
  // Besides being logged in, you may restrict access to only certain users   
  based on an ID established when they login. 
  // Parse the strings into arrays. 
  $arrUsers = Explode(",", $strUsers); 
  $arrGroups = Explode(",", $strGroups); 
 if (in_array($UserName, $arrUsers)) { 
  $isValid = true; 
  } 
  // Or, you may restrict access to only certain users based on their 
 username. 
 if (in_array($UserGroup, $arrGroups)) { 
  $isValid = true; 
} 
if (($strUsers == "") && false) { 
  $isValid = true; 
} 
} 
return $isValid; 
}

$MM_restrictGoTo = "Login.php";
if (!((isset($_SESSION['MM_Username'])) &&     
 (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'],  
 $_SESSION['MM_UserGroup'])))) {   
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" .  
urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo); 
exit;
}
?>
<?php
 if (!function_exists("GetSQLValueString")) {
 function GetSQLValueString($theValue, $theType, $theDefinedValue = "",   
 $theNotDefinedValue = "") 
{
 if (PHP_VERSION < 6) {
 $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 }

 $theValue = function_exists("mysql_real_escape_string") ?   
 mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
 case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;    
case "long":
case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  break;
case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  break;
case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;
case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  break;
 }
 return $theValue;
 }
 }

 if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",  
$theNotDefinedValue = "") 
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ?  
mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;    
case "long":
case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  break;
case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  break;
case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;
case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  break;
}
return $theValue;
}
}

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",   
$theNotDefinedValue = "") 
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ?   
mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;    
case "long":
case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  break;
case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  break;
case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;
case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  break;
}
return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

 $editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "cprtc")) {
$insertSQL = sprintf("INSERT INTO usr_ans (u_name, topic, q_id, ansr) VALUES  
(%s, %s, %s, %s)",
                   GetSQLValueString($_POST['uname'], "text"),
                   GetSQLValueString($_POST['topc'], "text"),
                   GetSQLValueString($_POST['qid'], "int"),
                   GetSQLValueString($_POST['RadioGroup1'], "text"));

 mysql_select_db($database_localhost, $localhost);
 $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error());
}

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",    
$theNotDefinedValue = "") 
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

 $theValue = function_exists("mysql_real_escape_string") ?  
 mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
 case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;    
case "long":
case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  break;
case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  break;
case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;
case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  break;
}
return $theValue;
}
}

$colname_User = "-1";
if (isset($_SESSION['MM_Username'])) {
$colname_User = $_SESSION['MM_Username'];
}
mysql_select_db($database_localhost, $localhost);
$query_User = sprintf("SELECT * FROM `user` WHERE Username = %s",  
 GetSQLValueString($colname_User, "text"));
$User = mysql_query($query_User, $localhost) or die(mysql_error());
$row_User = mysql_fetch_assoc($User);
$totalRows_User = mysql_num_rows($User);

 $maxRows_qusnqz = 2;
 $pageNum_qusnqz = 0;
 if (isset($_GET['pageNum_qusnqz'])) {
  $pageNum_qusnqz = $_GET['pageNum_qusnqz'];
 }
 $startRow_qusnqz = $pageNum_qusnqz * $maxRows_qusnqz;

 mysql_select_db($database_localhost, $localhost);
 $query_qusnqz = "SELECT * FROM quesns WHERE topic = 'C Language' ORDER BY 
 q_id ASC";
 $query_limit_qusnqz = sprintf("%s LIMIT %d, %d", $query_qusnqz,  
 $startRow_qusnqz, $maxRows_qusnqz);
 $qusnqz = mysql_query($query_limit_qusnqz, $localhost) or    
 die(mysql_error());
 $row_qusnqz = mysql_fetch_assoc($qusnqz);

if (isset($_GET['totalRows_qusnqz'])) {
$totalRows_qusnqz = $_GET['totalRows_qusnqz'];
 } else {
 $all_qusnqz = mysql_query($query_qusnqz);
  $totalRows_qusnqz = mysql_num_rows($all_qusnqz);
 }
 $totalPages_qusnqz = ceil($totalRows_qusnqz/$maxRows_qusnqz)-1;

  $queryString_qusnqz = "";
 if (!empty($_SERVER['QUERY_STRING'])) {
 $params = explode("&", $_SERVER['QUERY_STRING']);
 $newParams = array();
 foreach ($params as $param) {
 if (stristr($param, "pageNum_qusnqz") == false && 
    stristr($param, "totalRows_qusnqz") == false) {
  array_push($newParams, $param);
 }
}
if (count($newParams) != 0) {
$queryString_qusnqz = "&" . htmlentities(implode("&", $newParams));
}
}
 $queryString_qusnqz = sprintf("&totalRows_qusnqz=%d%s", $totalRows_qusnqz,   
 $queryString_qusnqz);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="CSS/Layout 5.css" rel="stylesheet" type="text/css" />
<link href="CSS/Menu.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>Practise Quiz of C</title>
<script src="SpryAssets/SpryValidationRadio.js" type="text/javascript">   
 </script>
 <link href="SpryAssets/SpryValidationRadio.css" rel="stylesheet"   
 type="text/css" />
 </head>

 <body>
<div id="Holder">
<div id="Header"></div>
 <div id="NavBar">
<nav>
 <ul><li> <a href="Home.php" >Home</a></li>
   <li> <a href="Login.php" class="active">Login</a></li>
   <li> <a href="Regster.php"> Register</a></li>
   <li><a href="ForgetPassword.php"> Forgot Password</a></li>

  </ul>
 </nav>
</div>
<div id="Content">
 <div id="PageHeading">
   <h1>Welcome, <?php echo $row_User['Fname']; ?> <?php echo   
  $row_User['Lname']; ?></h1>
  </div>
 <div id="ContentLeft">
  <h2>Account Links:</h2>
  <h6>&nbsp;</h6>
  <h6># <a href="Update.php">Update Account Info</a></h6>
  <h6># <a href="Logout.php">Logout</a></h6>
  <h6><br />
  </h6>
 </div>
 <div id="ContentRight">
  <?php if ($totalRows_qusnqz > 0) { // Show if recordset not empty ?>
    <?php do { ?>
      <form action="<?php echo $editFormAction; ?>" id="cprtc" name="cprtc" 
         method="POST">
        <table width="620" border="0" cellpadding="1">
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><input name="qid" type="hidden" id="qid" value="<?php echo  
  $row_qusnqz['q_id']; ?>" />
              <input name="uname" type="hidden" id="uname" value="<?php echo 
  $row_User['Username']; ?>" />
              <input name="topc" type="hidden" id="topc" value="<?php echo 
  $row_qusnqz['topic']; ?>" /></td>
          </tr>
          <tr>
            <td><strong>Question <?php echo ($startRow_qusnqz + 1) ?>:  
  </strong><strong>
              <label for="qsn"></label>
            </strong><?php echo $row_qusnqz['quesn']; ?></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="spryradio1">
              <label>
                <input type="radio" name="RadioGroup1" value="A" 
     id="RadioGroup1_0" />
                A</label>
              ) <?php echo $row_qusnqz['A']; ?><br />
              <br />
              <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="radio" name="RadioGroup1" value="B"  
      id="RadioGroup1_1" />
                B</label>
              ) <?php echo $row_qusnqz['B']; ?><br />
              <br />
              <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="radio" name="RadioGroup1" value="C" 
      id="RadioGroup1_2" />
                C</label>
              ) <?php echo $row_qusnqz['C']; ?><br />
              <br />
              <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="radio" name="RadioGroup1" value="D" 
       id="RadioGroup1_3" />
                D</label>
              ) <?php echo $row_qusnqz['D']; ?><br />
              <span class="radioRequiredMsg">Please make a selection.</span> 
    </span></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td align="center" valign="middle"><input name="Submit" 
 type="submit" class="styletxtfld" id="Submit" value="Submit" />                   
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr align="center">
            <td>        
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

 <?php if ($pageNum_qusnqz < $totalPages_qusnqz) { // Show if not last page 
         ?>
       <a href="<?php printf("%s?pageNum_qusnqz=%d%s", $currentPage, min(4, 
     $pageNum_qusnqz + 1), $queryString_qusnqz); ?>"               
     class="styletxtfld">Next&nbsp;&nbsp;</a>
                <?php } // Show if not last page ?>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
        </table>
        <input type="hidden" name="MM_insert" value="cprtc" />
      </form>
      <?php } while ($row_qusnqz = mysql_fetch_assoc($qusnqz)); ?>
    <?php } // Show if recordset not empty ?>
    </div> 
 </div>
<div id="Footer"> 
 <h4>@Rituraj | <a href="AdminLogin.php" class="styletxtfld">Admin</a></h4>
 </div>
 </div>
<script type="text/javascript">
 var spryradio1 = new Spry.Widget.ValidationRadio("spryradio1");
</script>
</body>
</html>
<?php
 mysql_free_result($User);

  mysql_free_result($qusnqz);
  ?>}

1 个答案:

答案 0 :(得分:0)

您可以将用户问题位置存储在会话或数据库中,然后在回答问题时递增此值。

然后,您将检查用户问题位置并检索正确的问题,或者如果找不到位置,则返回第一个问题并将其问题位置设置为一个。

<?php
session_start();

if(!isset($_SESSION['user_question'])) $_SESSION['user_question'] = 1;

if(isset($_POST['next_question']) && $_POST['next_question']) {
    $_SESSION['user_question'] ++;

    // code to save question results in database
}

function get_next_question() {

    // code to retreive the next question data
}

get_next_question();
?>