<?php
include('../include/config.php');
error_reporting(0);
session_start();
$iduser=$_SESSION['user_idd'];
$date = new DateTime("now", new DateTimeZone('Africa/Nairobi') );
$timenow=$date->format('Y-m-d H:i:s');
$timenow2=$date->format('Y-m-d');
$month=$date->format('M');
$em='';
$stu='';

if(isset($_POST['pid']))
{
$pid=addslashes($_POST['pid']);
$ma=addslashes($_POST['ma']);
$en=addslashes($_POST['en']);
$kis=addslashes($_POST['kis']);
$sis=addslashes($_POST['sis']);
$ss=addslashes($_POST['ss']);

$angr=addslashes($_POST['angr']);
$bs=addslashes($_POST['bs']);
$lsk=addslashes($_POST['lif']);
$tech=addslashes($_POST['tech']);
$he=addslashes($_POST['he']);
$cre=addslashes($_POST['cre']);
$pe=addslashes($_POST['pe']);
$opt=addslashes($_POST['opt']);
$mks=addslashes($_POST['mks']);


$exa=addslashes($_POST['exa']);
$grd=addslashes($_POST['grd']);

$query=mysqli_query($con,"select * FROM pupils where count='$pid'");
while($row=mysqli_fetch_array($query))
{
$stu=addslashes($row['Names']);
}


$query=mysqli_query($con,"select * from Gradeseven where Grad='$grd' AND EID='$exa'");
$num=mysqli_fetch_array($query);
if($num>0)
{
$sql=mysqli_query($con,"UPDATE Gradeseven SET Eng='$en',Mat='$ma',Kis='$kis',SS='$ss',ISc='$sis',BS='$bs',Ang='$angr',LS='$lsk',PreT='$tech',HE='$he',CRE='$cre',PE='$pe',Mks='$mks',OptSub='$opt' WHERE EID='$exa' AND PID='$pid'");


if($sql)
{
$msg='updated exam score for '.$stu;
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msg','$timenow','$timenow2','update','$month')");

echo "Marks updated";
}else
{
echo 'Unable to save marks';
}    
}else
{

$sqll=mysqli_query($con,"UPDATE pupils SET EID='$exa' where Claz='$grd'");
$query3=mysqli_query($con,"INSERT Gradeseven(PID,Grad,EID) SELECT count,Claz, EID FROM pupils WHERE Claz='$grd' AND Status='Active'");

$sql=mysqli_query($con,"UPDATE Gradeseven SET Eng='$en',Mat='$ma',Kis='$kis',SS='$ss',ISc='$sis',BS='$bs',Ang='$angr',LS='$lsk',PreT='$tech',HE='$he',CRE='$cre',PE='$pe',Mks='$mks',OptSub='$opt' WHERE EID='$exa' AND PID='$pid'");

if($sql)
{
$msg='added exam score for '.$stu;
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msg','$timenow','$timenow2','save','$month')");

echo "success";
}else
{
echo 'Unable to save marks';
} 


}

}
else
{

}
?>