<?php
    session_start();
    include '../prijava/connect.php';
    setlocale(LC_TIME,"hr_HR");
    date_default_timezone_set("Europe/Zagreb");
    
    if ($dbc) {
        $added = new DateTime(date("Y-m-d H:i:s"));
        $added = $added->format('Y-m-d H:i:s');
        $name = $_SESSION['name'];
        $date = $_GET['date'];
        $menu = $_GET['menu'];
        $query = "SELECT * FROM orders WHERE date = '$date' AND user = '$name'";
        $result = mysqli_query($dbc,$query);
        if (mysqli_num_rows($result) == 0) {
            $query = "INSERT INTO orders (user, date, menu, added) VALUES ('$name', '$date', '$menu', '$added')";
            $result = mysqli_query($dbc,$query) or die('Error querying database.');
        } else {
            $query = "UPDATE orders SET menu = '$menu', added = '$added' WHERE date = '$date' AND user = '$name'";
            $result = mysqli_query($dbc,$query) or die('Error querying database.');
        }
    }
?>