未定义的索引"主题"用PHP

时间:2014-05-01 15:25:49

标签: php

我正在PHP使用Wampserver制作在线考试系统。这是我的"subject choose"页面。这里的$ user来自登录页面。它运作良好。即使带有相应单选按钮的主题名称也会出现。但错误在于 $selected_button=$_POST['subject'];。我准备了许多具有相同逻辑的其他页面。我不知道我哪里错了。我用谷歌搜索了几次,但无法帮助我。 提前致谢。 。 。

<html>
<body>
<?PHP
session_start();
$i=1;
$j=1;
$user_name = "root";
$password = "";
$database = "online_exam";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database,$db_handle);
if ($db_found)
  {
  //$user = $_POST['user'];
  $user = $_SESSION['u'];
  $SQL = "SELECT Std_name,Std_course FROM student_details WHERE Std_Id='$user'";
  $result = mysql_query($SQL);
  $db_field = mysql_fetch_array($result);
         $name = $db_field['Std_name'] ;
     //session_start();
        $cour= $db_field['Std_course'];
    $_SESSION['$cour'] = $db_field['Std_course'];            
   print "Welcome ".$name.". You are in ".$cour." <BR>";
  //<BR><BR>
  $SQL = "SELECT distinct(subj) FROM ques_ans WHERE course='$cour'";
  $result = mysql_query($SQL);
  $num_row = mysql_num_rows($result);
  print "Choose your subject in which you want to give exam"."<BR>";
  echo '<FORM name ="form1" method ="post" action= "exam_a.php">';
  while($db_field = mysql_fetch_array($result)){
$sub[$i]= $db_field['subj'];
?>
<Input type = 'Radio' Name ='subject' id='subject' value= 'subject<?PHP print $i?>'><?PHP print @$sub[$i]?><BR>
<?php
        $i++;
  }
  $selected_button=$_POST['subject'];
while($j<$i)
    {     
      if($selected_button=='subject'.$j)
            $_SESSION['$sub']= $sub[$j];
     $j++;
    }
  print $_SESSION['$sub']; 
  echo  '<Input type = "submit" Name ="b_subject"  value= "SELECT"></FORM>';
  mysql_close($db_handle);
}
?>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

subject是一个单选按钮,如果用户不打算选择任何单选按钮,那么该字段就不会被发送到服务器。

解决方案1 ​​

选择默认主题:

<Input type = 'Radio' checked Name ='subject' id='subject' value= 'subject<?PHP print $i?>'>
  <?PHP   print @$sub[$i]?>

解决方案2

检查是否选中了单选按钮,如果没有在php中进行默认选择

if(!isset($_POST['subject'])){
$selected_button="default button";
}else{
  $selected_button=$_POST['subject'];
}