<?php
include "dbconnect.php";
if (isset($_POST['diataktiko']))
{
	$ergo=array();
	$dn=$_POST['dn'];
	$rn=$_POST['rn'];
	$din=$_POST['din'];
	$ergo=$_POST['ergo'];//array
	$xoros=$_POST['xoros'];//array
	$pn=$_POST['pn'];
	$new=$_POST['new'];
	if ($new==1)
	{
		$q="insert into diataktiko (dnum,dreal,datein) values ('$dn','$rn','$din')";
		if (!mysqli_query($con, $q)) 
			{
				echo "Error: " . $q . "<br>" . mysqli_error($con);
			}
		$i=0;
		
		foreach ($ergo as $erg)
		{
		  
		  if( trim($erg) <> '' )
			{
			$item=$i+1;echo $item;
			$q="insert into diadetail (dnum,item,jobreq,person,xoros) values ('$dn','$item','$erg','$pn[$i]','$xoros[$i]')";
		
			if (!mysqli_query($con, $q)) 
			{
				echo "Error: " . $q . "<br>" . mysqli_error($con);
			}
			$i++;
		  }
		}
	}
	else
	{
		$i=0;
		foreach ($ergo as $erg)
		{
		  if( trim($erg) <> '' )
			{
			$item=$i+1;
			$q="update diadetail SET jobreq='$erg',person='$pn[$i]',xoros='$xoros[$i]' where dnum='$dn' and item='$item'";
		
			if (!mysqli_query($con, $q)) 
			{
				echo "Error: " . $q . "<br>" . mysqli_error($con);
			}
			$i++;
		  }
		}

	}
	header("Location: diataktiko1.php");
}