<?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']);
$ea =addslashes($_POST['ea']);


$en1 =addslashes($_POST['en']);
$kis1 =addslashes($_POST['kis']);
$ma1 =addslashes($_POST['ma']);
$cre1 =addslashes($_POST['cre']);
$ca1 =addslashes($_POST['ca']);
$ea1 =addslashes($_POST['ea']);


if($en1==''){$en1==0;}
if($kis1==''){$kis1==0;}
if($ma1==''){$ma1==0;}
if($cre1==''){$cre1==0;}
if($ca1==''){$ca1==0;}
if($ea1==''){$ea1==0;}

$tot=$en1+$kis1+$ma1+$cre1+$ca1+$ea1;


$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',EA='$ea',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,EA) VALUES('$eid','$cla','$ma','$en','$kis','$cre','$user','$ca','$ea')");
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';
}
}
}
?>