<?php
include "../koneksi.php";

$planid = $_POST['planid'];
$shift = $_POST['shift'];
$jenis = $_POST['jenis'];
$dt_id = $_POST['dt_id'];
$dtm = $_POST['mydtm'];
$time = $_POST['mytime'];

$hitung = count($dt_id);

for( $i=0; $i < $hitung; $i++ ) {
	if($dtm[$i]!==''||$time[$i]!=='') {
	if($dt_id[$i]!=='') {
		$query = "UPDATE downtime SET Reason = '$dtm[$i]',
									`Time` = '$time[$i]'
									WHERE DT_ID = '$dt_id[$i]'";

			mysqli_query($koneksi, $query)
			or die ("Gagal Perintah SQL".mysql_error());
	} else {
    $query = "insert INTO downtime SET
										PlanID = '$planid',
										Shift = '$shift',
										Jenis = '$jenis',
										Reason = '$dtm[$i]',
										`Time` = '$time[$i]'
										";

	mysqli_query($koneksi, $query)
	or die ("Gagal Perintah SQL".mysql_error());
	}
	} else {
	if($dt_id[$i]!=='') {
		$query = "DELETE FROM downtime 
							WHERE DT_ID ='$dt_id[$i]' 
								";

		mysqli_query($koneksi, $query)
		or die ("Gagal Perintah SQL".mysql_error());
	}else{

	}

	}
}

header('Location:lap_prod.php?id='.$planid.'&shift='.$shift);

?>

