PHP脚本仅在我刷新页面时才能解决

时间:2018-04-28 17:42:05

标签: php

在PHP测验脚本中,它仅在页面刷新一次时有效。

它发生在第一个问题后,从第二个问题PHP脚本工作正常如何解决这个问题。

我添加了一个自动刷新页面的PHP脚本,但每个问题都令人耳目一新,但不是必须的,我只对第一个问题有疑问。

Quiz.php

<?php
session_start();
include("database.php");
extract($_POST);
extract($_GET);
extract($_SESSION);
/*$rs=mysqli_query($cn,"select * from mst_question where test_id=$tid",) or die(mysqli_connect_error());
if($_SESSION[qn]>mysqli_num_rows($rs))
{
    unset($_SESSION[qn]);
    exit;
}*/

if(isset($subid) && isset($testid)){
    $_SESSION[sid]=$subid;
    $_SESSION[tid]=$testid;
    header("location:quiz.php");
}

if(!isset($_SESSION[sid]) || !isset($_SESSION[tid])){
    header("location: index.php");
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Online Quiz</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="quiz.css" rel="stylesheet" type="text/css">
</head>

<body>
<?php
include("header.php");

$query="select * from mst_question";

$rs=mysqli_query($cn,"select * from mst_question where test_id=$tid") or die(mysqli_connect_error());
if(!isset($_SESSION[qn])){
    $_SESSION[qn]=0;
    mysqli_query($cn,"delete from mst_useranswer where sess_id='" . session_id() ."'") or die(mysqli_connect_error());
    $_SESSION[trueans]=0;
}else{
    if($submit=='Next Question' && isset($ans)){
        mysqli_data_seek($rs,$_SESSION[qn]);
        $row = mysqli_fetch_row($rs);   
        mysqli_query($cn,"insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysqli_connect_error());
        if($ans==$row[7]){
            $_SESSION[trueans]=$_SESSION[trueans]+1;
        }
        $_SESSION[qn]=$_SESSION[qn]+1;
    }else if($submit=='Get Result' && isset($ans)){
        mysqli_data_seek($rs,$_SESSION[qn]);
        $row = mysqli_fetch_row($rs);   
        mysqli_query($cn,"insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysqli_connect_error());
        if($ans==$row[7]){
            $_SESSION[trueans]=$_SESSION[trueans]+1;
        }
        echo "<h1 class=head1> Result</h1>";
        $_SESSION[qn]=$_SESSION[qn]+1;
        echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]";
        echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans];
        $w = $_SESSION[qn]-$_SESSION[trueans];
        echo "<tr class=fans><td>Wrong Answer<td> ". $w;
        echo "</table>";
        mysqli_query($cn,"insert into mst_result(login,test_id,test_date,score) values('$login','$tid',NULL,'$_SESSION[trueans]')") or die(mysqli_connect_error());
        echo "<h1 align=center><a href=review.php> Review Question</a> </h1>";
        unset($_SESSION[qn]);
        unset($_SESSION[sid]);
        unset($_SESSION[tid]);
        unset($_SESSION[trueans]);
        exit;
    }
}

$rs = mysqli_query($cn,"select * from mst_question where test_id=$tid") or die(mysqli_connect_error());

if($_SESSION[qn]>mysqli_num_rows($rs)-1)
{
    unset($_SESSION[qn]);
    echo "<h1 class=head1>Some Error  Occured</h1>";
    session_destroy();
    echo "Please <a href=index.php> Start Again</a>";
    exit;
}

mysqli_data_seek($rs,$_SESSION[qn]);

$row = mysqli_fetch_row($rs);
echo "<form name=myfm method=post action=quiz.php>";
echo "<table width=100%> <tr> <td width=30>&nbsp;<td> <table border=0>";

$n = $_SESSION[qn]+1;
echo "<tR><td><span class=style2>Que ".  $n .": $row[2]</style>";
echo "<tr><td class=style8><input type=radio name=ans value=1>$row[3]";
echo "<tr><td class=style8> <input type=radio name=ans value=2>$row[4]";
echo "<tr><td class=style8><input type=radio name=ans value=3>$row[5]";
echo "<tr><td class=style8><input type=radio name=ans value=4>$row[6]";

if($_SESSION[qn]<mysqli_num_rows($rs)-1){
    echo "<tr><td><input type=submit name=submit value='Next Question'></form>";
}else{
    echo "<tr><td><input type=submit name=submit value='Get Result'></form>";
    echo "</table></table>";
}
?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

@arkascha和@Mehdi感谢您的帮助。我的问题解决了。查询$ tid和$ testid中的两个单词存在混淆我用$ testid替换了两个单词并且我的代码正常工作