title: Online quiz website by using PHP, MySQL and HTML
body: <p>I'm designing a website for online quiz system. I have made the database and few of webpages for login, question adding and all. </p>
  
  <p>Now I am facing a problem in the following code. In this quiz page, I have one submit button and one link for the next question.</p>
  
  <pre><code>submit: to store the user option in database
  next: to fetch the next question from database
  </code></pre>
  
  <p>Now I want that functionality of submit button is to be done by next link that is when I click the next then the user answer must be saved in the database and the next question must came from the database. How can I do this? </p>
  
  <p>My code for that page is:</p>
  
  <pre><code>{&lt;?php @session_start(); ?&gt;
  &lt;?php require_once('Connections/localhost.php'); ?&gt;
  &lt;?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 == "") &amp;&amp; false) { 
    $isValid = true; 
  } 
  } 
  return $isValid; 
  }
  
  $MM_restrictGoTo = "Login.php";
  if (!((isset($_SESSION['MM_Username'])) &amp;&amp;     
   (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'],  
   $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&amp;";
  if (isset($_SERVER['QUERY_STRING']) &amp;&amp; strlen($_SERVER['QUERY_STRING']) &gt; 0) 
  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" .  
  urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
  }
  ?&gt;
  &lt;?php
   if (!function_exists("GetSQLValueString")) {
   function GetSQLValueString($theValue, $theType, $theDefinedValue = "",   
   $theNotDefinedValue = "") 
  {
   if (PHP_VERSION &lt; 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 &lt; 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 &lt; 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"])) &amp;&amp; ($_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 &lt; 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("&amp;", $_SERVER['QUERY_STRING']);
   $newParams = array();
   foreach ($params as $param) {
   if (stristr($param, "pageNum_qusnqz") == false &amp;&amp; 
      stristr($param, "totalRows_qusnqz") == false) {
    array_push($newParams, $param);
   }
  }
  if (count($newParams) != 0) {
  $queryString_qusnqz = "&amp;" . htmlentities(implode("&amp;", $newParams));
  }
  }
   $queryString_qusnqz = sprintf("&amp;totalRows_qusnqz=%d%s", $totalRows_qusnqz,   
   $queryString_qusnqz);
  ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
   &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;link href="CSS/Layout 5.css" rel="stylesheet" type="text/css" /&gt;
  &lt;link href="CSS/Menu.css" rel="stylesheet" type="text/css" /&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
   &lt;title&gt;Practise Quiz of C&lt;/title&gt;
  &lt;script src="SpryAssets/SpryValidationRadio.js" type="text/javascript"&gt;   
   &lt;/script&gt;
   &lt;link href="SpryAssets/SpryValidationRadio.css" rel="stylesheet"   
   type="text/css" /&gt;
   &lt;/head&gt;
  
   &lt;body&gt;
  &lt;div id="Holder"&gt;
  &lt;div id="Header"&gt;&lt;/div&gt;
   &lt;div id="NavBar"&gt;
  &lt;nav&gt;
   &lt;ul&gt;&lt;li&gt; &lt;a href="Home.php" &gt;Home&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt; &lt;a href="Login.php" class="active"&gt;Login&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt; &lt;a href="Regster.php"&gt; Register&lt;/a&gt;&lt;/li&gt;
     &lt;li&gt;&lt;a href="ForgetPassword.php"&gt; Forgot Password&lt;/a&gt;&lt;/li&gt;
  
    &lt;/ul&gt;
   &lt;/nav&gt;
  &lt;/div&gt;
  &lt;div id="Content"&gt;
   &lt;div id="PageHeading"&gt;
     &lt;h1&gt;Welcome, &lt;?php echo $row_User['Fname']; ?&gt; &lt;?php echo   
    $row_User['Lname']; ?&gt;&lt;/h1&gt;
    &lt;/div&gt;
   &lt;div id="ContentLeft"&gt;
    &lt;h2&gt;Account Links:&lt;/h2&gt;
    &lt;h6&gt;&amp;nbsp;&lt;/h6&gt;
    &lt;h6&gt;# &lt;a href="Update.php"&gt;Update Account Info&lt;/a&gt;&lt;/h6&gt;
    &lt;h6&gt;# &lt;a href="Logout.php"&gt;Logout&lt;/a&gt;&lt;/h6&gt;
    &lt;h6&gt;&lt;br /&gt;
    &lt;/h6&gt;
   &lt;/div&gt;
   &lt;div id="ContentRight"&gt;
    &lt;?php if ($totalRows_qusnqz &gt; 0) { // Show if recordset not empty ?&gt;
      &lt;?php do { ?&gt;
        &lt;form action="&lt;?php echo $editFormAction; ?&gt;" id="cprtc" name="cprtc" 
           method="POST"&gt;
          &lt;table width="620" border="0" cellpadding="1"&gt;
            &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;&lt;input name="qid" type="hidden" id="qid" value="&lt;?php echo  
    $row_qusnqz['q_id']; ?&gt;" /&gt;
                &lt;input name="uname" type="hidden" id="uname" value="&lt;?php echo 
    $row_User['Username']; ?&gt;" /&gt;
                &lt;input name="topc" type="hidden" id="topc" value="&lt;?php echo 
    $row_qusnqz['topic']; ?&gt;" /&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;&lt;strong&gt;Question &lt;?php echo ($startRow_qusnqz + 1) ?&gt;:  
    &lt;/strong&gt;&lt;strong&gt;
                &lt;label for="qsn"&gt;&lt;/label&gt;
              &lt;/strong&gt;&lt;?php echo $row_qusnqz['quesn']; ?&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span id="spryradio1"&gt;
                &lt;label&gt;
                  &lt;input type="radio" name="RadioGroup1" value="A" 
       id="RadioGroup1_0" /&gt;
                  A&lt;/label&gt;
                ) &lt;?php echo $row_qusnqz['A']; ?&gt;&lt;br /&gt;
                &lt;br /&gt;
                &lt;label&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                  &lt;input type="radio" name="RadioGroup1" value="B"  
        id="RadioGroup1_1" /&gt;
                  B&lt;/label&gt;
                ) &lt;?php echo $row_qusnqz['B']; ?&gt;&lt;br /&gt;
                &lt;br /&gt;
                &lt;label&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                  &lt;input type="radio" name="RadioGroup1" value="C" 
        id="RadioGroup1_2" /&gt;
                  C&lt;/label&gt;
                ) &lt;?php echo $row_qusnqz['C']; ?&gt;&lt;br /&gt;
                &lt;br /&gt;
                &lt;label&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                  &lt;input type="radio" name="RadioGroup1" value="D" 
         id="RadioGroup1_3" /&gt;
                  D&lt;/label&gt;
                ) &lt;?php echo $row_qusnqz['D']; ?&gt;&lt;br /&gt;
                &lt;span class="radioRequiredMsg"&gt;Please make a selection.&lt;/span&gt; 
      &lt;/span&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td align="center" valign="middle"&gt;&lt;input name="Submit" 
   type="submit" class="styletxtfld" id="Submit" value="Submit" /&gt;                   
        &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
     &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr align="center"&gt;
              &lt;td&gt;        
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
    &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
  
   &lt;?php if ($pageNum_qusnqz &lt; $totalPages_qusnqz) { // Show if not last page 
           ?&gt;
         &lt;a href="&lt;?php printf("%s?pageNum_qusnqz=%d%s", $currentPage, min(4, 
       $pageNum_qusnqz + 1), $queryString_qusnqz); ?&gt;"               
       class="styletxtfld"&gt;Next&amp;nbsp;&amp;nbsp;&lt;/a&gt;
                  &lt;?php } // Show if not last page ?&gt;
    &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;&amp;nbsp;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/table&gt;
          &lt;input type="hidden" name="MM_insert" value="cprtc" /&gt;
        &lt;/form&gt;
        &lt;?php } while ($row_qusnqz = mysql_fetch_assoc($qusnqz)); ?&gt;
      &lt;?php } // Show if recordset not empty ?&gt;
      &lt;/div&gt; 
   &lt;/div&gt;
  &lt;div id="Footer"&gt; 
   &lt;h4&gt;@Rituraj | &lt;a href="AdminLogin.php" class="styletxtfld"&gt;Admin&lt;/a&gt;&lt;/h4&gt;
   &lt;/div&gt;
   &lt;/div&gt;
  &lt;script type="text/javascript"&gt;
   var spryradio1 = new Spry.Widget.ValidationRadio("spryradio1");
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php
   mysql_free_result($User);
  
    mysql_free_result($qusnqz);
    ?&gt;}
  </code></pre>