<?php
/**
* @project: ImgAnnotations
* @author: Bram Prieshof
*/
?>

<style>
    a.button {
    background-color: Gainsboro;
    border: none;
    color: black;
    padding: 10px 10px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    }
</style>

<?php
include 'db.php';
//Check if data was send
if (empty($_POST["TheNewData"])) {
    echo 'No data has been submitted <br><button onclick="history.back()">Go Back</button>';
}
else {
    $stmt = $mysqli->prepare("SELECT TRUE FROM ImgAnnotations WHERE id = ? LIMIT 1"); 
    $stmt->bind_param("i", $_POST["TheID"]);
    $stmt->execute();
    $result = $stmt->get_result(); // get the mysqli result
    if( mysqli_num_rows($result) > 0) {
        //ID does exits, updating";
        $stmt = $mysqli->prepare("UPDATE ImgAnnotations SET data = ? WHERE id = ?");
        $stmt->bind_param("si", $_POST["TheNewData"], $_POST["TheID"]);
    }else {
        //ID doesn't exits, creating";
        $stmt = $mysqli->prepare("INSERT INTO ImgAnnotations (id, data) VALUES (?, ?)");
        $stmt->bind_param("is", $_POST["TheID"], $_POST["TheNewData"]);
    }
    $stmt->execute();

    echo 'Data has been saved<br> <a href="Editor.php" class="button">Editor</a><a> </a><a href="Viewer.php" class="button">Viewer</a>';
}
?>