<?php

$db = new mysqli("localhost", "matej", "database112", "data");

if (mysqli_connect_errno()) {
    echo "Can not connect to database";
    exit();
}

if ($_POST["save"] == "true") {

    if (!$_POST["late"]) {
        $_POST["late"] = 0;
    }

    if (!$_POST["step"]) {
        $_POST["step"] = 0;
    }

    $data = mysqli_query($db, "INSERT INTO canvas (canvas, delay, step, unit, direction) VALUES ('" . serialize($_POST["canvas"]) . "', '" . $_POST["late"] . "', '" . $_POST["step"] . "', '" . $_POST["time"] . "', '" . $_POST["direction"] . "')");

    if ($data) {
        echo "success";
    }
    else {
        echo "fail";
    }
    //echo $_POST["direction"];

    /*echo $data['direction'];
    echo $data['late'];
    echo $data['time'];
    echo $data['step'];
    echo $data['canvas'];*/
}
else {
    $data = mysqli_query($db, "SELECT temp1, press, humidity FROM sensor_data ORDER BY tdate DESC, ttime DESC LIMIT 1");
    echo json_encode(mysqli_fetch_array($data));
}

?>
