-2

I am making online exam system in PHP using Wampserver. This is my "subject choose" page. Here the $user is coming from the login page. It is working well. Even the subject names with the corresponding radio buttons are coming as well. But the error lies in $selected_button=$_POST['subject'];. I prepare many other pages with same logic. I don't know where I am wrong. I googled it several times but couldn't help me out. Thanks in advance. . . .

<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>
ArtemGr
  • 11,684
  • 3
  • 52
  • 85
  • 2
    `print_r($_POST);` — Do you see it there? – Amal Murali May 01 '14 at 15:26
  • 1
    Sidenote: `mysql_*` family of functions have been deprecated and will be removed in future. I suggest you switch to PDO or MySQLi instead. Also note that you're vulnerable to [SQL injection](http://bobby-tables.com). If you're planning to switch to MySQLi / PDO, you can use prepared statements to prevent this problem. – Amal Murali May 01 '14 at 15:28
  • *sidenote* `session_start()` wont work if there is already output. `` – Lawrence Cherone May 01 '14 at 15:29
  • mike thanks for reply..but 'b_subject' is the submit button..and I want to fetch the value from radio. – user3587561 May 01 '14 at 15:31
  • If no radio button is checked `$_POST['subject']` won't be set, so `$selected_button=$_POST['subject']` will result in an undefined index error ... and are you really storing database connection strings in a publicly accessible file? One small server misconfiguration and you're potentially completely screwed. – CD001 May 01 '14 at 15:34
  • can you print_r($_POST) . – CodeIsLife May 01 '14 at 15:34

1 Answers1

1

subject is a radio button and in your case if the user is not going to select any radio button, then that field is not going to be sent to the server.

SOLUTION 1

make the default subject be selected:

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

SOLUTION 2

check if the radio button is selected, if not make a default selection in php

if(!isset($_POST['subject'])){
$selected_button="default button";
}else{
  $selected_button=$_POST['subject'];
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
ashishmaurya
  • 1,196
  • 10
  • 18