简体   繁体   English

使用 PHP 和 MYSQL 的测验系统

[英]Quiz System using PHP and MYSQL

Every Thing is working fine for my online quiz system but page of uploading questions is not working as it should be working.我的在线测验系统一切正常,但上传问题的页面无法正常工作。

The user is restricted to add 20 questions at a time if the limit exceeds, a message will prompted and he will be redirected to his account.如果超过限制,用户一次只能添加 20 个问题,系统会提示一条消息,并将他重定向到他的帐户。

This is the form which will allow the user to input his question, four options and a correct option.这是允许用户输入他的问题、四个选项和一个正确选项的表格。

<html>
<body>
<form action="be_uploadquiz.php" method="post">
<table><tr><td>Enter Question Here</td>
<td>
<input name="question" type="text" maxlength="100" /></td></tr>
<tr><td>Enter First Option</td>
<td>
<input name="opt1" type="text" maxlength="100" /></td></tr>
<tr><td>Enter Second Option</td><td>
<input name="opt2" type="text" maxlength="100" /></td></tr>
<tr><td>Enter Third Option</td>
<td>
<input name="opt3" type="text" maxlength="30" /></td></tr>
<tr><td>Enter Fourth Option</td>
<td>
<input name="opt4" type="text" maxlength="30" /></td></tr>
<tr><td>Select The Correct Option</td>
<td>
<select name="woptcode">
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
</td></tr>
<tr><td>
<input name="submit" type="submit" value="Next" />
</td></tr></table></form>
</body>
</html>

Here is the uploadquiz.php file which inserts the questions这是插入问题的uploadquiz.php文件

    <?php
session_start();
$link = mysql_connect("localhost","root","");
mysql_select_db("quiz",$link);
$question = $_POST['question'];
$opt1 = $_POST['opt1'];
$opt2 = $_POST['opt2'];
$opt3 = $_POST['opt3'];
$opt4 = $_POST['opt4'];
$woptcode = $_POST['woptcode'];
if ( isset( $_POST['submit'] ) ) {
    $sql = "INSERT INTO be_quiz (question,opt1,opt2,opt3,opt4,woptcode) VALUES ('$question', '$opt1','$opt2','$opt3','$opt4','$woptcode')";
    $i++;
 header('Location:be_uploadquiz.html');
if($i==20)
{
    header('Location:message.html');
}
}
session_destroy();
if(!mysql_query($sql))
{
    die('Error:'.mysql_error());
}

mysql_close();
?>

I want the user to redirect again to uploadquiz.html if the limit is not reached and to a file message.html if the maximum limit (ie 20 questions have been reached) is reached and then to his account.如果未达到限制,我希望用户再次重定向到uploadquiz.html,如果达到最大限制(即已达到20 个问题),则重定向到文件message.html,然后重定向到他的帐户。 this is not working need help.这不起作用需要帮助。

Your variable $i is not maintained across navigation.您的变量$i不在导航中维护。 You could use a session variable for that, like this:您可以为此使用会话变量,如下所示:

start_session();
if (!isset($_SESSION["counter"])) {
    $_SESSION["counter"] = 0;
}

Then use $_SESSION["counter"] instead of $i ;然后使用$_SESSION["counter"]而不是$i

$_SESSION["counter"]++

Don't destroy the session, or you will not retain this value.不要销毁会话,否则您将不会保留此值。 So delete this line:所以删除这一行:

destroy_session();

If you want to make sure sessions are destroyed after a certain time of inactivity (also resetting the counter for that user), then read here how you can do that.如果您想确保会话在一段时间不活动后被销毁(也为该用户重置计数器),请在此处阅读如何执行此操作。

Now there is still an issue: your check on the 20-limit happens too late and would not stop the user from continuing to submit.现在仍然存在一个问题:您对 20 限制的检查发生得太晚了,并且不会阻止用户继续提交。 You should put that test before the actual insert and increment happens.您应该在实际insert和增量发生之前进行该测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM