<?php
session_start();

include('../include/config.php');
$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');


if(isset($_POST['id']))
{
$user=$_SESSION['user_idd'];
$eid =addslashes($_POST['id']);
$cla =addslashes($_POST['cla']);

$en =addslashes($_POST['en']);
$kis =addslashes($_POST['kis']);
$ma =addslashes($_POST['ma']);
$cre =addslashes($_POST['cre']);
$ca =addslashes($_POST['ca']);
$ins =addslashes($_POST['ins']);
$an =addslashes($_POST['an']);
$ss=addslashes($_POST['ss']);


$en1 =addslashes($_POST['en']);
$kis1 =addslashes($_POST['kis']);
$ma1 =addslashes($_POST['ma']);
$cre1 =addslashes($_POST['cre']);
$ca1 =addslashes($_POST['ca']);
$ins1 =addslashes($_POST['ins']);
$an1 =addslashes($_POST['an']);
$ss1=addslashes($_POST['ss']);


if($en==''){$en1==0;}
if($kis==''){$kis1==0;}
if($ma==''){$ma1==0;}
if($cre==''){$cre1==0;}
if($ca==''){$ca1==0;}
if($ins==''){$ins1==0;}
if($an==''){$an1==0;}
if($ss==''){$ss1==0;}

$tot=$en1+$kis1+$ma1+$cre1+$ca1+$ins1+$an1+$ss1;


$query=mysqli_query($con,"select * from examsetting where examid='$eid' AND  Grad='$cla'");
$num=mysqli_fetch_array($query);
if($num>0)
{
$sql=mysqli_query($con,"UPDATE examsetting SET MA='$ma',EN='$en',KS='$kis',CRE='$cre',CA='$ca',ST='$ins',SS='$ss',AN='$an',User='$user',TT='$tot' WHERE examid='$eid' AND Grad='$cla'");
if($sql)
{
    $msg='updated exam details for '.$cla;
 $sql2=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month) VALUES('$iduser','$msg','$timenow','$timenow2','save','$month') ");
 if($sql2)
 {
    echo 'success'; 
 }
}else
{
echo 'Sorry. Error occured while saving data';
}

}else
{

$sql=mysqli_query($con,"INSERT INTO examsetting(examid,Grad,MA,EN,KS,CRE,User,CA,ST,SS,AN) VALUES('$eid','$cla','$ma','$en','$kis','$cre','$user','$ca','$ins','$ss','$an')");
if($sql)
{
    $msg='updated exam details for '.$cla;
 $sql2=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month) VALUES('$iduser','$msg','$timenow','$timenow2','save','$month') ");
 if($sql2)
 {
    echo 'success'; 
 }
}else
{
    echo 'Sorry. Error occured while saving data';
}
}
}else if(isset($_POST['pt']))
{
    
$user=$_SESSION['user_idd'];
$eid =addslashes($_POST['eid']);
$cla =addslashes($_POST['cla']);
$en =addslashes($_POST['en']);
$kis =addslashes($_POST['kis']);
$ma =addslashes($_POST['ma']);
$cre =addslashes($_POST['cre']);
$ca =addslashes($_POST['ca']);
$ins =addslashes($_POST['ins']);
$an =addslashes($_POST['an']);
$ss=addslashes($_POST['ss']);  
$pt=addslashes($_POST['pt']); 


$en1 =addslashes($_POST['en']);
$kis1 =addslashes($_POST['kis']);
$ma1 =addslashes($_POST['ma']);
$cre1 =addslashes($_POST['cre']);
$ca1 =addslashes($_POST['ca']);
$ins1 =addslashes($_POST['ins']);
$an1 =addslashes($_POST['an']);
$ss1=addslashes($_POST['ss']);
$pt1=addslashes($_POST['pt']); 


if($en==''){$en1==0;}
if($kis==''){$kis1==0;}
if($ma==''){$ma1==0;}
if($cre==''){$cre1==0;}
if($ca==''){$ca1==0;}
if($ins==''){$ins1==0;}
if($an==''){$an1==0;}
if($ss==''){$ss1==0;}
if($pt==''){$pt1==0;}

$tot=$en1+$kis1+$ma1+$cre1+$ca1+$ins1+$an1+$ss1+$pt1;


$query=mysqli_query($con,"select * from examsetting where examid='$eid' AND  Grad='$cla'");
$num=mysqli_fetch_array($query);
if($num>0)
{
$sql=mysqli_query($con,"UPDATE examsetting SET MA='$ma',EN='$en',KS='$kis',CRE='$cre',CA='$ca',ST='$ins',SS='$ss',AN='$an',PT='$pt',User='$user',TT='$tot' WHERE examid='$eid' AND Grad='$cla'");
if($sql)
{
    $msg='updated exam details for '.$cla;
 $sql2=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month) VALUES('$iduser','$msg','$timenow','$timenow2','save','$month') ");
 if($sql2)
 {
    echo 'success'; 
 }
}else
{
echo 'Sorry. Error occured while saving data';
}

}else
{

$sql=mysqli_query($con,"INSERT INTO examsetting(examid,Grad,MA,EN,KS,CRE,User,CA,ST,SS,AN,PT) VALUES('$eid','$cla','$ma','$en','$kis','$cre','$user','$ca','$ins','$ss','$an','$pt')");
if($sql)
{
    $msg='updated exam details for '.$cla;
 $sql2=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month) VALUES('$iduser','$msg','$timenow','$timenow2','save','$month') ");
 if($sql2)
 {
    echo 'success'; 
 }
}else
{
    echo 'Sorry. Error occured while saving data';
}
}











}else
{
   
}


?>