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

    filename:    pitaddupdateprocess.php
    description: add or update pit
    inputs:      pitId and associated parameters
    outputs:     none
    actions:     none
    redirection: pitlist.php

********************************************************************************
*/
    include 'functions.php';
/*
    ... connect to database
*/
    $conn = connectToDB();
/*
    ... get entered parameters
*/
    $add = $_POST["add"];
    $update = $_POST["update"];
    $pitId = $_GET["pitId"];
    $manufacturer = $_POST["manufacturer"];
    $model = $_POST["model"];
    $blowerP = (int) $_POST["blowerP"];
    $blowerI = (int) $_POST["blowerI"];
    $blowerD = (int) $_POST["blowerD"];
    $ventP = (int) $_POST["VentP"];
    $ventI = (int) $_POST["VentI"];
    $ventD = (int) $_POST["VentD"];
    $comments = $_POST["comments"];
/*
    ... add pit to database
*/
    if ($add) {
        $sql = "INSERT INTO pits (manufacturer, model, blowerP, blowerI, blowerD, ventP, ventI, ventD, comments) VALUES ('$manufacturer', '$model', '$blowerP', '$blowerI', '$blowerD', '$ventP', '$ventI', '$ventD', '$comments')";
        if (!mysqli_query($conn, $sql)) {
            error_log("Error: " . $sql . " " . mysqli_error($conn));
        }
/*
    ... update pit in database
*/
   } elseif ($update) {
        $sql = "manufacturer='$manufacturer', model='$model', blowerP=$blowerP, blowerI=$blowerI, blowerD=$blowerD, ventP=$ventP, ventI=$ventI, ventD=$ventD, comments='$comments'";
        $sql = "UPDATE pits SET " . $sql . " WHERE pitId = $pitId";
        if (!mysqli_query($conn, $sql)) {
            error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
        }
   } else {
        error_log("pitaddupdateprocess.php: no action");
   }
/*
    ... close database connection and redirect to pit list
*/
    mysqli_close($conn);           
    header('Location: pitlist.php');
?>