使用php和sql会话的在线测验系统

时间:2013-03-01 15:40:49

标签: php mysql session random

我目前正在开发一个在线测验系统,该系统将值用户答案存储在数据库中,但仅存储标记而非问题。在测验页面中,使用php的随机函数在10个问题中选择了一个问题。我正在创建会话每次用户登录并在会话变量中存储变量。请帮助我作为这个网络世界的新手..

我面临以下问题: -

1 - 现在,每当我尝试将答案存储在数据库中时,它会递增一次,如果刷新页面,它会再次递增一次,这是我刷新的时间..这是可耻的,有什么方法可以避免这种情况并增加仅标记用户一次。

2.第二件事是如何在测验页面中选择下一个问题,即每次选择一个1/10问题而不刷新页面同时隐藏其他问题,就像我们在测验系统中使用php而不使用随机函数一样

我的代码是 - 在页面上使用4个问题 -

<?php
session_start();
$username=$_SESSION['username'];
$total=$_SESSION['total'];
$q1=isset($_POST['q1']);
$q2=isset($_POST['q2']);
$q3=isset($_POST['q3']);
$q4=isset($_POST['q4']);
$q5=isset($_POST['q5']);
$full=30;
echo 'Welcome '.$username;
echo $total;

function quiz(){
$set=rand(1,5);
$total=0;

echo $set;
 switch($set)
 {

 case 1:

 echo '<form action="quiz.php" method="post" align="center">
<table align="center">
<tr><label name="question" id="ques"><h1 >ISM stands for?</h1></label><tr>
<tr>
<td align="right"><input type="radio" name="q1" value="A"></td>
<td>Indian School Of Mines</input><br></td>
</tr>

<tr>
<td align="right"><input type="radio" name="q1" value="B"></td>
<td >Indian Science Of Mines</input><br></td>
</tr>

<tr>
<td align="right"><input type="radio" name="q1" value="C"></td>
<td>Indian School Of Mineral</input><br></td>
</tr>

<tr >
<td align="right"><input type="radio" name="q1" value="D"></td>
<td>Indian School Of Machines</input><br></td>
</tr>

<tr >


<td align="center"><br><input type="submit" name="snext" value="Save/Next"><br></td>
</tr>

</form>';



if(isset($_POST['q1'])=='A')
{
$total =$total + 1;
include ('process.php');

}
else{

}

break;

case 2:

 echo '<form action="quiz.php" method="post" align="center" id="">
<table align="center">
<tr><label name="question" id="ques"><h1 >HTML stands for?</h1></label><tr>
<tr>
<td align="right"><input type="radio" name="q2" value="A"></td>
<td>Indian School Of Mines</input><br></td>
</tr>

<tr>
<td align="right"><input type="radio" name="q2" value="B"></td>
<td >Indian Science Of Mines</input><br></td>
</tr>

<tr>
<td align="right"><input type="radio" name="q2" value="C"></td>
<td>Indian School Of Mineral</input><br></td>
</tr>

<tr >
<td align="right"><input type="radio" name="q2" value="D"></td>
<td>Indian School Of Machines</input><br></td>
</tr>

<tr >



<td align="center"><br><input type="submit" name="snext" value="Save/Next"><br></td>
</tr>

</form>';


if(isset($_POST['q2'])=='C')
{
$total =$total + 1;
include ('process.php');

}
else{

}
break;


case 3:

 echo '<form action="quiz.php" method="post" align="center">
<table align="center">
<tr><label name="question" id="ques"><h1 >CSS stands for?</h1></label><tr>
<tr>
<td align="right"><input type="radio" name="q3" value="A"></td>
<td>Indian School Of Mines</input><br></td>
</tr>

<tr>
<td align="right"><input type="radio" name="q3" value="B"></td>
<td >Indian Science Of Mines</input><br></td>
</tr>

<tr>
<td align="right"><input type="radio" name="q3" value="C"></td>
<td>Indian School Of Mineral</input><br></td>
</tr>

<tr >
<td align="right"><input type="radio" name="q3" value="D"></td>
<td>Indian School Of Machines</input><br></td>
</tr>

<tr >


<td align="center"><br><input type="submit" name="snext" value="Save/Next"><br></td>
</tr>

</form>';
if(isset($_POST['q3'])=='D')
{
$total =$total + 1;
include ('process.php');

}
else{

}
break;


case 4:

 echo '<form action="quiz.php" method="post" align="center">
<table align="center">
<tr><label name="question" id="ques"><h1 >PHP stands for?</h1></label><tr>
<tr>
<td align="right"><input type="radio" name="q4" value="A"></td>
<td>Indian School Of Mines</input><br></td>
</tr>

<tr>
<td align="right"><input type="radio" name="q4" value="B"></td>
<td >Indian Science Of Mines</input><br></td>
</tr>

<tr>
<td align="right"><input type="radio" name="q4" value="C"></td>
<td>Indian School Of Mineral</input><br></td>
</tr>

<tr >
<td align="right"><input type="radio" name="q4" value="D"></td>
<td>Indian School Of Machines</input><br></td>
</tr>

<tr >


<td align="center"><br><input type="submit" name="snext" value="Save/Next"><br></td>
</tr>
</table>
</form>';
if(isset($_POST['q4'])=='D')
{
$total =$total + 1;
include ('process.php');

}
else{

}
break;



default:

Echo "This is quiz application";

break;

}

}

?>

<html>
<head>
</head>

<body>
<?php echo quiz(); ?>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

就像一个小小的提示开始:

使用

if(isset($_POST['q1']) && $_POST['q1']=='A'){
  ...
}

isset($_POST['q1'])只检查是否设置了$_POST['q1']