<?php
session_start();
include "logo.php";
date_default_timezone_set("Europe/Athens");
?>

<!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=utf-8" />
<script type='text/javascript'>
window.onunload = function(){
window.opener.location.reload();}
</script>
</head>

<body>
<?php 
require("dbconnect.php");
$mitroo=$_SESSION['mitroo']; 
$usr=$_SESSION['usr'];
$tetramino=$_SESSION['tetramino'];
if (isset($_POST['Submitddk'])) 
{
    $dt = $_SESSION['dt'];
	$dt= date('Y-m-d', strtotime($dt));
	$dt = mysqli_real_escape_string($con, $dt);
	$bathmos =$_POST['bathm'];
	$i=1;
	foreach( $bathmos  as $value ) {
		    $key="f".$i; 
            $fields[$i] = "`$key`";
            if ($value==NULL)
            {
             $values[$i]=0;
            }
            else {$values[$i] = $value;}
			$i++;
    }
    
	$fields = implode(",", $fields);
    $values = implode(",", $values);
    //print_r($values);
    //print_r($fields);
	$result = mysqli_query($con,"SELECT * FROM ddk WHERE mitroo='$mitroo'");
	$c=mysqli_num_rows($result);
	
	if ($c == 0) 
	 {
	   $sql= "INSERT INTO ddk (mitroo,$fields) VALUES ('$mitroo',$values)" ;   
	   if (!mysqli_query($con,$sql)){die('Error: ' . mysqli_error($con));}    
	 }  
     else
	 {
		
       $sql= "DELETE FROM ddk WHERE mitroo='$mitroo'" ;
	   if (!mysqli_query($con,$sql)){die('Error: ' . mysqli_error($con));    } 
	   $sql= "INSERT INTO ddk (mitroo,$fields) VALUES ('$mitroo',$values)" ;  
	   if (!mysqli_query($con,$sql)){die('Error: ' . mysqli_error($con));    }
	 }
}
// ............ display the total marks
$result = mysqli_query($con,"SELECT * FROM ddk WHERE mitroo='$mitroo'");
$row=mysqli_fetch_array($result); 
$n=mysqli_num_fields($result)-1;
$sum=0;
for ($i=1;$i<=$n;$i++)
	{	
	    $field="f".$i;
		$sum=$sum+$row["$field"];
	}
echo "<br>";echo "<br>";
echo " ΣΥΝΟΛΟ ΒΑΘΜΟΛΟΓΙΑΣ ΔΔΚ = ".$sum;
?> 



</body>
</html>