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

    filename:     probeaddupdateprocess.php
    description:  add/update/cancel probe
    inputs:       probeId, add, update, cancel
    outputs:      probeId
    actions:      none
    redirections: probelist.php

********************************************************************************
*/
    include 'functions.php';
    $conn = connectToDB();
/*
    ... get entered parameters
*/
    $probeId = $_GET["probeId"];
    $add = $_POST["add"];
    $update = $_POST["update"];
    $cancel= $_POST["cancel"];
    $manufacturer = $_POST["manufacturer"];
    $model = $_POST["model"];
    $a = (float) $_POST["a"];
    $b = (float) $_POST["b"];
    $c = (float) $_POST["c"];
/*
    ... add probe to database
*/
    if ($add) {
        $sql = "INSERT INTO probes (manufacturer, model, a, b, c) VALUES ('$manufacturer', '$model', $a, $b, $c)";
        if (!mysqli_query($conn, $sql)) {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
        }
        $probeId = mysqli_insert_id($conn);
/*
    ... update probe in database
*/
    } elseif ($update) {
        $sql = "manufacturer='$manufacturer', model='$model', a=$a, b=$b, c=$c";
        $sql = "UPDATE probes SET " . $sql . " WHERE probeId = $probeId";
        if (!mysqli_query($conn, $sql)) {
            error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
        }
   } elseif ($cancel) {
   } else {
        error_log("probeaddupdateprocess.php: no action");
   }
/*
    ... close database connection and redirect to pit list
*/
    mysqli_close($conn);           
    header('Location: probelist.php?probeId=' . $probeId);
?>