Jump to content

why I getting undefined index in my code


Terza22

Recommended Posts

undefined index grade in line 108 


<?php
if (!isset($_SESSION)) 
{
  session_start();
}

IF(isset($_SESSION['username']))
ECHO "WELL COME ".$_SESSION['username']."<BR>";
else IF(isset($_SESSION['Name']))
if(isset($_POST['button']))
  {
    $correct=0;
    $wrong=0;
    $sum=0;
    $grade='';
    $total_q=@$_POST['total'];
    for($i=1;$i<=$total_q;$i++)
    {
    $con = mysql_connect("localhost","root");
mysql_select_db("oes", $con);

 $sql = "SELECT * from question_page where question='".@$_POST['q'.$i]."' and Answer='".@$_POST['ans'.$i]."'";// where schedule.status='active' 
 $result = mysql_query($sql,$con);
 $result1 = mysql_fetch_array($result);
if(mysql_num_rows($result))
    $correct++;
    $wrong=$_POST['total']-$correct;
    $sum=($correct*10)/$_POST['total'];
    //echo "question of page:".@$_POST['q'.$i]." <br>answer of user ".@$_POST['ans'.$i]."<br>";
    //echo "question of db:".$result1['question']." <br>answer of db ".$result1['Answer']."<br><br>";
    if($sum>=9 && $sum<=10)
    {
    $grade="A+";
    }
    else if($sum>=8.5 && $sum<9)
    {
    $grade="A";
    }
    else if($sum>=8 && $sum<8.5)
    {
    $grade="A-";
    }
    else if($sum>=7.5 && $sum<8)
    {
    $grade="B+";
    }
    else if($sum>=7 && $sum<7.5)
    {
    $grade="B";
    }
    else if($sum>=6.5 && $sum<7)
    {
    $grade="B-";
    }
    else if($sum>=6 && $sum<6.5)
    {
    $grade="C+";
    }
    else if($sum>=5 && $sum<6)
    {
    $grade="C";
    }
    else if($sum>=4.5 && $sum<5)
    {
    $grade="C-";
    }
    else if($sum>=4 && $sum<4.5)
    {
    $grade="D";
    }
    else if($sum>=3.5 && $sum<4)
    {
    $grade="Fx";
    }
    else if($sum>=0 && $sum<3.5)
    {
    $grade="F";
    }
    }
    echo "total number of question ".$_POST['total']."<br>";
    echo "total number of correct answer".$correct."<br>";
    echo "total number of wrong answer".$wrong."<br>";
    echo "total number of Marks:".$sum."<br>";
    echo "your grade is:".$grade."<br>";
}
else
{
 header('location:Exam.php');
}
?>

<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css">
<style type="text/css">

</style>

<div id="right">
<?php
$cnct=mysql_connect("localhost","root","")or die("not connected");
$qry=mysql_select_db("oes") or die("not connected");
$crsnm=mysql_query("select course_name from schedule where schedule_id='".$_GET['Id']."'");
$data=mysql_fetch_array($crsnm);
$a=$_SESSION['ID'];
$b=$_SESSION['Name'];
$c=$_POST['total'];
$g=$_POST['grade'];
$con=mysql_connect("localhost","root","")or die("not connected");
$query=mysql_select_db("oes") or die("note selected");
$res="insert into result values('$a','$b','$correct','$wrong','$sum','$c', '$g' ,'yes','".$data['course_name']."')";
$sql=mysql_query($res) or die(mysql_error());
if(!$sql)
{
echo"not inserted ".mysql_error()."<br>";
}
else
{
echo"inserted"."<br>"."<br>";    
}
?>

<?php
$a=$_SESSION['ID'];
$b=$_SESSION['Name'];
$c=$_POST['total'];
$d=$_POST['grade'];
 $con=mysql_connect("localhost","root","")or die("not connected");
$query=mysql_select_db("oes") or die("note selected");
$sql=mysql_query("insert into result values('$a','$b','$correct','$wrong','$sum','$c','$d')") or die(mysql_error());
if(!$sql)
{
echo"not inserted ".mysql_error()."<br>";
}
else
{
echo"inserted"."<br>"."<br>";    
}
mysql_close($con);
?>

 <br/-->
                  
 <div align="center">
  </div>
                    
<td bgcolor="#c24122">&nbsp;</td>
</tr>
 </table>
 </div>
 </div>

and it calculate the result from exam.php which are below as follows:


<?php
if (!isset($_SESSION)) 
{
 session_start();
  
}
IF(isset($_SESSION['username']))
ECHO "WELL COME ".$_SESSION['username']."<BR>";
else IF(isset($_SESSION['Name']))
    //ECHO "WELL COME ".$_SESSION['Name']."<BR>";
//ECHO "YOUR ID IS ".$_SESSION['ID']."<BR>";
$var=$_SESSION['ID'];
 //echo$_SESSION['Name']; 
?>

<?php

$sql=mysql_connect("localhost","root","")or die("not connected");
mysql_select_db("oes");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title></title>
<style type="text/css">
<!--
.style1 {
    color: #FFFFFF;
    font-weight: bold;
}
-->
</style>
</head>

<body>
<div id="container">
    <?php
        include "Header.php";
        ?>
    <div id="content">
    
        <div id="left">
        <rights>
        
        
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <?php
    /* echo $_SESSION['co1']."<br>";
     echo $_SESSION['co2']."<br>";
     echo $_SESSION['co3']; */
     $con=mysql_connect("localhost","root","")or die('not selected');
     mysql_select_db("oes",$con)or die('not selected');
     $sql="select * from schedule";
     $query=mysql_query($sql);
     while($row = mysql_fetch_array($query))
     {
    $sub= $coname=$row['course_name'];
  //echo $sub."<br>";
     }
 $gu=$var;
 //echo $gu;
 mysql_close($con);
$con=mysql_connect("localhost","root","")or die("not connected");
$query=mysql_select_db("oes",$con)or die("not selected");
$sql= "select Stud_ID from result where Stud_ID='".$gu."' AND status='yes'";
$result = mysql_query($sql);
//$date= $row_Recordsetd['exam_date'];
//if(!mysql_num_rows($result))
     //if((!mysql_num_rows($result)))
  {
     
?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$dateFormat = "d F Y -- g:i a";

$targetDate = time() + (1*60);//Change the 25 to however many minutes you want to countdown

$actualDate = time();

$secondsDiff = $targetDate - $actualDate;

$remainingDay    = floor($secondsDiff/60/60/24);

$remainingHour  = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);

$remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);

$remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));

$actualDateDisplay = date($dateFormat,$actualDate);

$targetDateDisplay = date($dateFormat,$targetDate);

?>
<?php 
$con1 = mysql_connect("localhost","root");
mysql_select_db("oes", $con1);
$Recordsetd = mysql_query("SELECT exam_time From schedule where schedule_id='".@$_GET['Id']."'") or die(mysql_error());
$row_Recordsetd = mysql_fetch_array($Recordsetd);
//$ex_tih=$row_Recordsetd['exam_time'][strlen($row_Recordsetd['exam_time'])-8].$row_Recordsetd['exam_time'][strlen($row_Recordsetd['exam_time'])-7];
//$ex_tim=$row_Recordsetd['exam_time'][strlen($row_Recordsetd['exam_time'])-5].$row_Recordsetd['exam_time'][strlen($row_Recordsetd['exam_time'])-4];
$ex_tis=$row_Recordsetd['exam_time'][strlen($row_Recordsetd['exam_time'])-2].$row_Recordsetd['exam_time'][strlen($row_Recordsetd['exam_time'])-1];
            
            ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<head>

 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Quiz System</title>
<script type="text/javascript">

  var days = <?php echo $remainingDay; ?> 

  var hours = <?php echo $ex_tih; ?> 

  var minutes = <?php echo $ex_tim; ?> 

  var seconds = <?php echo $ex_tis; ?>
  

function setCountDown ()

{

  seconds--;
  if (seconds < 0){

      minutes--;

      seconds = 60

  }

  if (minutes < 0)
  {

      hours--;

      minutes = 59
  }

  if (hours < 0)
  {
      days--;
      hours = 23
  }
  document.getElementById("remain").innerHTML = days+" days, "+hours+" hours, "+minutes+" minutes, "+seconds+" seconds";

  SD=window.setTimeout( "setCountDown()", 1000 );

  if (minutes == '00' && seconds == '00') { //seconds = "00"; window.clearTimeout(SD);
      {
           //document.getElementById('file2').style.visibility = 'hidden';
            window.location = "check.php";
         //document.getElementById('OptionA').disabled = true;
      }
        //window.alert("Time is up. Press OK to continue."); // change timeout message as required
//$('input').disabled = true;
           
          

}

    } 
 
</script>
</head>
<body onload="setCountDown();">
 <!--Start Time: <!?php echo $actualDateDisplay; ?><br />

 End Time:<!?php echo $targetDateDisplay; ?><br /-->

<h2><b>  <div id="remain"><?php echo "$remainingDay days, $remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?></div></b></h2>
            <h1> <!?php echo $_SESSION['Name'];?></h1>
                 <p>
<form id="form2" name="form2" method="post" action="point.php?Id=<?php if(isset($_GET['Id']))echo $_GET['Id'];?>">
        <!--input type="text" name="course" value="<!?php if(isset($_GET['Id']))echo $_GET['Id'];?-->   
 <?php
$SID=@$_GET['Id'];
// Establish Connection with Database
$con1 = mysql_connect("localhost","root");
// Select Database
mysql_select_db("oes", $con1);

//$query_Recordsetd = ;
$Recordsetd = mysql_query("SELECT course_name From schedule where schedule_id='".@$_GET['Id']."'") or die(mysql_error());
$row_Recordsetd = mysql_fetch_array($Recordsetd);

    
$con = mysql_connect("localhost","root");

mysql_select_db("oes", $con);

$sql = "SELECT * from question_page where course_name='".$row_Recordsetd[0]."'";// where schedule.status='active' 
$result = mysql_query($sql,$con);
$i=1;
while($row = mysql_fetch_array($result))
{
   $Id=$row['exam_id'];
   $Question=$row['question'];
   $OptionA=$row['Option1'];
   $OptionB=$row['Option2'];
  $OptionC=$row['Option3'];
  $OptionD=$row['Option4'];
//echo $Id;
?>
</p>


             
        <?php
        
        if($Id=='mult')
             {
        echo'     <td  colspan="2"><table width="100%" height="64" border="0" cellpadding="0" cellspacing="0" bordercolor="#A6BF79" say true or fales>
                      
                        <tr>
                          <td height="32" width="50%">('.$i.')<input type="text" style="width:1000%;background:none;border:hidden;" readonly name="q'.$i.'" value="'.$Question.'"></td>
                        </tr>

                        <tr>
                          <td height="32" width="50%"><input type="radio" name="ans'.$i.'" value="A" id="OptionA" />
                          '.$OptionA.'</td>
                          <td width="50%"><input type="radio" name="ans'.$i.'" value="B" id="OptionC" />
                              '.$OptionC.'</td>
                        </tr>
                        <tr>
                          <td height="32" width="50%"><input type="radio" name="ans'.$i.'" value="C" id="OptionB" />
                              '. $OptionB.'</td>
                          <td width="50%"><input type="radio" name="ans'.$i.'" value="D" id="OptionD" />
                              '.$OptionD.'</td>
                        </tr>
                      </table></td>';
                }
             if($Id=='T/F')
              {
                    echo '<tr>
                      <td>('.$i.')</td><td height="36" colspan="2" bgcolor="#85A157"><span class="style1"></span>
                      <tr>say True or False</tr>
                      <input type="text" name="ans'.$i.'" style="margin-left:4px;width:25%;background-color:white;border:none;" value=""></td>
                        <td><input type="text" style="width:auto;background:none;border:hidden;" readonly name="q'.$i.'" value="'.$Question.'"></td>
                  </tr>';
                    
                }
                if($Id=='Filling'){
                    
                    echo '<tr>
                      <td>('.$i.')</td><td height="36" colspan="2" bgcolor="#85A157"><span class="style1"></span>
                      <tr> Falling the space</tr>
                      <input type="text" name="ans'.$i.'" style="margin-left:4px;width:25%;background-color:white;border:none;" value=""></td>
                        <td><input type="text" style="width:auto;background:none;border:hidden;" readonly name="q'.$i.'" value="'.$Question.'"></td>
                  </tr>';
                }
               ?>      

    
 <p>
         <?php
        
$i=$i+1;                           
}
?>

 </p>
<p align="center">                          
</p>
<div align="center">
  <?php
// Retrieve Number of records returned
$records = mysql_num_rows($result);
if($records!=0)
{
?>
<input type="text" hidden name="total" value="<?php echo mysql_num_rows($result);?>">
  <input type="submit" name="button" id="button" value="Submit" />

  <?php
}  
// Close the connection
mysql_close($con);
}
/*elseif(!mysql_num_rows($result))
{
    echo 'not access the exam';
}
elseif(mysql_num_rows($result))
  {
    echo 'not aceess the exam';
  }
  else
  {
      echo" not access the exam";
  }*/
  
?>
         </div>
          </form>
          <p>&nbsp;</p>
          <h1>&nbsp;</h1>
      </div>
        
        <div id="footerline"></div>
        <div id="footer">Copyright &copy; 2017 JJU Online Examination System.<br/> All rights reserved.&nbsp;</div>
    </div>

</body>
</html>
 

UCScreenshot20170526040049.jpg

Link to comment
Share on other sites

Please use code tags when you post code on the forum, and if you're asking about a particular line it helps to identify the line.

An undefined index means that you're trying to access an array index that doesn't exist.  Maybe it's a typo, or maybe you're trying to get data from something like $_POST when a form wasn't submitted.  You can use isset to check if an array index is set before trying to access it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...