
<?php
    //setting header to json
    header('Content-Type: application/json');
    header("Access-Control-Allow-Origin: *");


    // $driverTypeID = $_POST["id"];  // get internal sensor id from js
    $driverName = $_POST["n"];  // get internal sensor id from js

    require '../php/database.php';

    $result = $mysqli->query("INSERT INTO `drivers` (`driverName`) VALUES ('".$driverName."')");
    // $result = $mysqli->query("INSERT INTO `drivers` (`driverTypeID`, `driverName`) VALUES ('".$driverTypeID."', '".$driverName."')");
    if ($mysqli->query($result) === TRUE) {
        echo "Record updated successfully";
    } 
    // else {
    //     echo "Error updating record: " . $mysqli->error;
    // }

    $query = sprintf("SELECT driverID FROM drivers ORDER BY driverID DESC LIMIT 1");

    //execute query
    $result = $mysqli->query($query);

    //loop through the returned data
    $dataWindow = array();
    foreach ($result as $row) {
        $dataWindow[] = $row;
    }

    //free memory associated with result
    $result->close();
    
    //close connection
    $mysqli->close();

    //print the data
    print json_encode($dataWindow); 
?>