这是MCQ考试代码。代码中的所有内容都在工作,因为$ p3 [8]我没有得到所需的输出。它也无法打印$ p3 [8]值。
<?php
session_start();
error_reporting(1);
include("connect.php");
extract($_POST);
extract($_GET);
extract($_SESSION);
$username = $_SESSION['student'];
if(isset($testid))
{
//$_SESSION[sid]=$subid;
//echo 'sads';
$_SESSION['tid']=$testid;
header("location:test.php");
}
if(!isset($_SESSION['tid']))
{//echo 'qwerwq';
header("location:home.php");
}
?>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Online Exam</title>
<link href="quiz.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
include("head.php");
$rs=mysqli_query($con, "select * from addmcq where test_id=$tid",$cn);
$r = mysqli_num_rows($rs);
if($r==0)
{
//header("location:test2.php");
}
if(!isset($_SESSION['qn']))
{
$_SESSION['qn']=0;
mysqli_query($con, "delete from mcq_user where sess_id='$username' and test_id=$tid") or die(mysql_error());
$_SESSION['trueans']=0;
}
else
{
if($submit=='Next MCQ' && isset($ans))
{
mysqli_data_seek($rs,$_SESSION['qn']);
$row= mysqli_fetch_row($rs);
mysqli_query($con, "delete from mcq_user where sess_id='$username', test_id=$tid and ques=$row[3]");
echo "ww";
mysqli_query($con, "insert into mcq_user(sess_id, test_id, que, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('$username', $tid,'$row[3]','$row[4]','$row[5]','$row[6]', '$row[7]','$row[8]','$ans')") or die(mysql_error());
if($ans==$row[8])
{
$_SESSION['trueans']=$_SESSION['trueans']+1;
}
$_SESSION['qn']=$_SESSION['qn']+1;
}
else if($submit=='Next EA' && isset($ans))
{
mysqli_data_seek($rs,$_SESSION['qn']);
$row= mysqli_fetch_row($rs);
mysqli_query($con, "delete from mcq_user where sess_id='$username', test_id=$tid and ques=$row[3]");
mysqli_query($con, "insert into mcq_user(sess_id, test_id, que, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('$username', $tid,'$row[3]','$row[4]','$row[5]','$row[6]', '$row[7]','$row[8]','$ans')") or die(mysql_error());
if($ans==$row[8])
{
$_SESSION['trueans']=$_SESSION['trueans']+1;
// echo $_SESSION['trueans'];
}
//echo "<h1 class=head1> Result</h1>";
$_SESSION['qn']=$_SESSION['qn']+1;
//$_SESSION['trueans'];
header("location:test2.php");
//echo '<br />Start short answer type questions<a href="test2.php">click here</a>';
}
else if($submit=='Previous MCQ' && isset($ans))
{
mysqli_data_seek($rs,$_SESSION['qn']);
$row= mysqli_fetch_row($rs);
mysqli_query($con, "insert into mcq_user(sess_id, test_id, que, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('$username', $tid,'$row[3]','$row[4]','$row[5]','$row[6]', '$row[7]','$row[8]','$ans')") or die(mysql_error());
if($ans==$row[8])
{
$_SESSION['trueans']=$_SESSION['trueans']+1;
}
$_SESSION['qn']=$_SESSION['qn']-1;
}
}
主要问题从这里开始$ p2也没有打印值
$rs=mysqli_query($con, "select * from addmcq where test_id=$tid",$cn) or die(mysql_error());
$r = mysqli_num_rows($rs);
$p1= mysqli_query($con, "select * from mcq_user where test_id=$tid and sess_id=$username");
$p2 = mysqli_num_rows($p1);
//echo "qqqqq";
echo $p2;
if($r==0)
{
header("location:test2.php");
}
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=head.php> Start Again</a>";
exit;
}
mysqli_data_seek($rs,$_SESSION['qn']);
mysqli_data_seek($p1,$_SESSION['qn']);
echo $_SESSION['qn'];
$row= mysqli_fetch_row($rs);
$ p3初始化并显示它以进行检查。
$p3= mysqli_fetch_row($p1);
echo $p3[8];
//include("tt.html");
echo "<form name=myfm method=post action=test.php>";
echo "<table width=100%> <tr> <td width=30> <td> <table border=0>";
$n=$_SESSION['qn']+1;
echo "<tr><td><span class=style2>Que ". $n .": $row[3]</style>";
echo "<tr><td class=style8><input ";
if($p3[8]==1) echo "checked='checked'";
echo " type=radio name=ans value=1>$row[4]";
echo "<tr><td class=style8> <input ";
if($p3[8]==2) echo "checked='checked'";
echo" type=radio name=ans value=2>$row[5]";
echo "<tr><td class=style8><input ";
if($p3[8]==3) echo "checked='checked'";
echo" type=radio name=ans value=3>$row[6]";
echo "<tr><td class=style8><input ";
if($p3[8]==4) echo "checked='checked'";
echo" type=radio name=ans value=4>$row[7]";
if($n == 1)
echo "<tr><td><input type=submit name=submit value='Next MCQ'><td><tr></form>";
if($_SESSION['qn']<mysqli_num_rows($rs)-1 && $n > 1)
echo "<tr><td><input type=submit name=submit value='Previous MCQ'><input type=submit name=submit value='Next MCQ'><td><tr></form>";
if($_SESSION['qn']== mysqli_num_rows($rs)-1)
echo "<tr><td><input type=submit name=submit value='Previous MCQ'><input type=submit name=submit value='Next EA'><td><tr></form>";
echo "</table></table>";
?>
</body>
</html>
答案 0 :(得分:-1)
确保此查询
$p1= mysqli_query($con, "select * from mcq_user where test_id=$tid and sess_id=$username");
将返回包含至少8列的记录......
还有一件事
如果$username
是一个字符串,那么你应该使用单引号
$p1= mysqli_query($con, "select * from mcq_user where test_id=$tid and sess_id='$username'");