在在线考试网站(php)中获取在数据库中具有价值的所选单选按钮

时间:2016-06-12 05:08:57

标签: php

我正在建立一个在线考试网站,我需要获取所选的单选按钮值来计算最终结果,以便向用户显示,我编码为:

<form method="POST" action="result1.php">

<div style="height:900px;width:1200px;color: black;margin-left:25px;">
<?php
 if($_SERVER["REQUEST_METHOD"]=="POST")
{
$servername="localhost";
$username="root";
$password="";
$dbname="online_exams";
$conn=mysql_connect($servername,$username,$password);
mysql_select_db($dbname,$conn);
$x=$_REQUEST['c1'];
$result=mysql_query("select q.qno,q.ques,a.a1,a.a2,a.a3,a.a4 from ques q,ans a where a.cid=q.cid and a.cid=$x and a.qno=q.qno",$conn);
$num_rows=mysql_num_rows($result);
if($num_rows>0)
{
while($row=mysql_fetch_array($result))
{

echo $row['qno'];
 echo"<br>";
echo $row['ques']; echo "<br/>";
?>

<input type="radio"  name="'<?php echo $row['qno']; ?>'"value="'<?php echo $row['a1']; ?>'" ><?php echo $row['a1'];  echo "<br/>"; ?>
<input type="radio" name="'<?php echo $row['qno']; ?>'" value="'<?php echo $row['a2']; ?>'" ><?php echo  $row['a2']; echo "<br/>";?>
<input type="radio" name="'<?php echo $row['qno']; ?>'" value="'<?php e  cho $row['a3']; ?>'" ><?php echo $row['a3']; echo "<br/>";?>
<input type="radio"  name="'<?php echo $row['qno']; ?>'"value="'<?php   echo $row['a4']; ?>'" ><?php echo $row['a4']; echo "<br/>";?> 

<?php

  }}}

?>

<input type="submit" name="submit" value="submit" >

然后在另一个页面来计算结果,虽然没有获取所需的结果值,因为radiobuttons的值存储在数据库中,我无法获得如何检索它们的任何方式,以下是其他代码:

<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$servername="localhost";
$username="root";
$password="";
$dbname="online_exams";
$conn=mysql_connect($servername,$username,$password);
mysql_select_db($dbname,$conn);

$result=mysql_query("select q.qno,q.cans,q.ques,a.a1,a.a2,a.a3,a.a4 from ques q,ans a where a.cid=q.cid and a.qno=q.qno",

$conn);


$x = 0;
$score = 0;

while ($row = mysql_fetch_assoc($result))
{
if(isset($_POST['$row['qno']']))
$answered = $_POST[' $row['qno']'];
$correct = $row['cans'] ;

if ($answered == $correct ) {
    $score++;
}


$x = $x + 1;
}
echo 'You had a total of ' . $score . ' out of ' . $x . ' questions right!';

}     ?&GT;

2 个答案:

答案 0 :(得分:0)

试试这个

<input type="radio"  name="<?php echo $row['qno']; ?>" value="<?php echo $row['a1']; ?>" ><?php echo $row['a1'];  echo "<br/>"; ?>
<input type="radio" name="<?php echo $row['qno']; ?>" value="<?php echo $row['a2']; ?>" ><?php echo  $row['a2']; echo "<br/>";?>
<input type="radio" name="<?php echo $row['qno']; ?>" value="<?php e  cho $row['a3']; ?>" ><?php echo $row['a3']; echo "<br/>";?>
<input type="radio"  name="<?php echo $row['qno']; ?>" value="<?php   echo $row['a4']; ?>" ><?php echo $row['a4']; echo "<br/>";?> 

我认为在“之后/之前使用单引号”会搞砸它。与值相同。由于您没有回显整行,因此无需使用引号将常量与变量分开。

基本上你有值=“'1'”而不是value =“1”

答案 1 :(得分:0)

你有一个语法错误,在这里更改,即使你编写一两行代码也使用括号,这是一个很好的做法

  if(isset($_POST[$row['qno']])){
      $answered = $_POST[$row['qno']];
  }