<?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');
$uni='0';
//$finaldate='Today: '.$date->format('l d M Y - H:i:s');

include('../include/config.php');

if(isset($_POST["id"]))
 {
 $name=addslashes($_POST["sn"]);
 $id=addslashes($_POST["sid"]);
 $gen=addslashes($_POST["gen"]);
 $phone=addslashes($_POST["phone"]);
 $edat=addslashes($_POST["edat"]);
 $subs=addslashes($_POST["subs"]);
 $em=addslashes($_POST["email"]);
 $cat=addslashes($_POST["cat"]);
 $idd=addslashes($_POST["id"]);
 $nh=addslashes($_POST["nh"]);
 $nat=addslashes($_POST["nat"]);
 $sta=addslashes($_POST["sta"]);
 $ns=addslashes($_POST["ns"]);
 //$spno=addslashes($_POST["spno"]);
 
 if($cat=='School Director' || $cat=='System User')
 {
  $uni='1';
 }

$query=mysqli_query($con,"SELECT * FROM Staffs WHERE IDNo='$id' AND id !='$idd'");
$num=mysqli_fetch_array($query);
if($num>0)
{
echo "Staff with above ID number has already been registered";
}else
{

$sql=mysqli_query($con,"UPDATE Staffs SET Specia='$uni',Name='$name',IDNo='$id',Phone='$phone',Gender='$gen',EmpDate='$edat',Subs='$subs',Status='$sta',Position='$cat',NHIF='$nh',Nature='$nat',Email='$em',NSSF='$ns' WHERE id='$idd'");
if($sql)
{
echo "success";
}else
{
echo "Error in saving data";
}
}
 }else
 {
if(!empty($_POST["sn"]))
 {
 $name=addslashes($_POST["sn"]);
 $id=addslashes($_POST["sid"]);
 $gen=addslashes($_POST["gen"]);
 $phone=addslashes($_POST["phone"]);
 $edat=addslashes($_POST["edat"]);
 $subs=addslashes($_POST["subs"]);
 $em=addslashes($_POST["email"]);
 $cat=addslashes($_POST["cat"]);
 $nh=addslashes($_POST["nh"]);
 $ns=addslashes($_POST["ns"]);
 $nat=addslashes($_POST["nat"]);
 
 if($cat=='School Director' || $cat=='System User')
 {
  $uni='1';
 }

$query=mysqli_query($con,"SELECT * FROM Staffs WHERE IDNo='$id' AND IDNo !=''");
$num=mysqli_fetch_array($query);
if($num>0)
{
echo "Staff with above ID number has already been registered";
}else
{
$sql=mysqli_query($con,"INSERT INTO Staffs(NSSF,Specia,Name,IDNo,Phone,Gender,EmpDate,Subs,RegDate,Email,Position,Nature,NHIF) values('$ns','$uni','$name','$id','$phone','$gen','$edat','$subs','$timenow2','$em','$cat','$nat','$nh')");
if($sql)
{
echo "success";
}else
{
echo "Error in saving data";
}
}	
 }
 }
?>