<?php

error_reporting(E_ERROR | E_PARSE);
// sql data connection 

$DB_Host = "localhost";
$DB_Name = "mrcode_FMdatabase";
$DB_Table = "Progress";
$DB_User = "mrcode_User";
$DB_Pass = "o;1fT@L+cB&P";



$docid = $_GET['docid'];
$forgeid =  $_GET['forgeid'];
$area = $_GET['area'];
$progress =  $_GET['progress'];
$urn = $_GET['urn'];



if($progress != null && $area != null){
    
    
// connect to sql 
$conn = new mysqli($DB_Host, $DB_User, $DB_Pass,$DB_Name);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// get data of element


$select = "SELECT Progress FROM Progress WHERE Forge_id = '$forgeid' AND urn = '$urn' " ;


$result = $conn->query($select);

if ($result->num_rows > 0) {
    
        $sql = 'UPDATE `Progress` SET `Progress`="' .$progress . '" WHERE Forge_id = "'.$forgeid.'" AND urn = "' .$urn.'"' ;

if ($conn->query($sql) === TRUE) {
        $row = $result->fetch_assoc();
        $myObj->updated = $row['Progress'];
        $myArray[] = $myObj;
        $myJSON = json_encode(array('Progress' =>$myArray));
        echo $myJSON;
        
    } 
    else {
        
        $myObj->updated = null;
        $myArray[] = $myObj;
        $myJSON = json_encode(array('Progress' =>$myArray));
        echo $myJSON;
    }

}
else{
    $sql = 'INSERT INTO `Progress`(`Forge_id`, `doc_id`, `urn`, `Area`, `Progress`) VALUES (\'' . $forgeid . '\',\'' . $docid . '\',\'' . $urn . '\',\'' . $area . '\',\'' . $progress . '\')';
    if ($conn->query($sql) === TRUE) {
        
        $myObj->updated = "OK";
        $myArray[] = $myObj;
        $myJSON = json_encode(array('Progress' =>$myArray));
        echo $myJSON;
        
    } 
    else {
        
        $myObj->updated = null;
        $myArray[] = $myObj;
        $myJSON = json_encode(array('Progress' =>$myArray));
        echo $myJSON;
    }
    
}
    


$conn->close();


}
else{
    
    $myObj->empty = null;
        $myArray[] = $myObj;
        $myJSON = json_encode(array('Progress' =>$myArray));
        echo $myJSON;
}

?>
