<?php 
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
require("../connect.php");
ini_set ("display_errors", "0");
error_reporting(E_ALL);
date_default_timezone_set('Asia/Jakarta');

$sqlcheck  = " SELECT * from app_entity_46 WHERE ";
$sqlcheck .= " field_672 = '".$_POST['division']."'";
$sqlcheck .= " and field_673 = '".$_POST['blokcode']."'";
$sqlcheck .= " and field_674 = '".$_POST['tphcode']."';";

$checkresult = $conn->query($sqlcheck);

if ($checkresult->num_rows == 0) {
	    
	$queryUploadTagTPH = "INSERT INTO app_entity_46 
	(parent_item_id, date_added, created_by, 
	field_671, field_672, field_673, 
	field_674, field_675, field_676, 
	field_677, field_678, field_679, field_2867) VALUES (
	1, UNIX_TIMESTAMP(), 20, 
	?, ?, ?, 
	?, ?, ?, 
	?, ?, ?, 318);";
    
    $stmtUploadTagTPH = mysqli_prepare($conn, $queryUploadTagTPH);
    mysqli_stmt_bind_param($stmtUploadTagTPH, 'sssssssss', 
    $_POST['barcodevalue'], $_POST['division'], $_POST['blokcode'], 
    $_POST['tphcode'], $_POST['tphcondition'], $_POST['pathcondition'], 
    $_POST['prunningcondition'], $_POST['latscan'], $_POST['longscan']);
    
    if (mysqli_stmt_execute($stmtUploadTagTPH)) {
    	echo json_encode(array("UPLOADTPH"=>"SUCCESS"));
	}
  
} else if ($checkresult->num_rows > 0) {
	mysqli_query($conn, "UPDATE app_entity_46 SET field_678 = '".$_POST['latscan']."', field_679 = '".$_POST['longscan']."' 
    WHERE field_672 = '".$_POST['division']."' AND field_673 = '".$_POST['blokcode']."' AND field_674 = '".$_POST['tphcode']."';");
	echo json_encode(array("UPLOADTPH"=>"SUCCESS"));
}

?>