<?php
/*
********************************************************************************

    filename:    foodaddupdateprocess.php
    description:
    inputs:      foodId and associated parameters
    outputs:     none
    actions:     none
    redirection: foodlist.php 

********************************************************************************
*/
            include 'functions.php';
            $conn = connectToDB();

            $add = $_POST["add"];
            $update = $_POST["update"];
            $foodId = $_GET["foodId"];
            $food = $_POST["food"];
            $doneness = $_POST["doneness"];
            $externalTemp = (int) $_POST["externalTemp"];
            $externalHighTemp = (int) $_POST["externalHighTemp"];
            $externalLowTemp = (int) $_POST["externalLowTemp"];
            $internalTemp = (int) $_POST["internalTemp"];
            $internalHighTemp = (int) $_POST["internalHighTemp"];
            $internalLowTemp = (int) $_POST["internalLowTemp"];
            $comments = $_POST["comments"];
            if ($add) {
                $sql = "INSERT INTO foods (food, doneness, externalTemp, externalHighTemp, externalLowTemp, internalTemp, internalHighTemp, internalLowTemp, comments) VALUES ('$food', '$doneness', '$externalTemp', '$externalHighTemp', '$externalLowTemp', '$internalTemp', '$internalHighTemp', '$internalLowTemp', '$comments')";
                if (!mysqli_query($conn, $sql)) {
                    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                }
           } elseif ($update) {
                $sql = "food='$food', doneness='$doneness', externalTemp=$externalTemp, externalHighTemp=$externalHighTemp, externalLowTemp=$externalLowTemp, internalTemp=$internalTemp, internalHighTemp=$internalHighTemp, internalLowTemp=$internalLowTemp, comments='$comments'";
                $sql = "UPDATE foods SET " . $sql . " WHERE foodId = $foodId";
                if (!mysqli_query($conn, $sql)) {
                    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                }
           }
            mysqli_close($conn);           
        header('Location: foodlist.php');
?>