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

    $a = $_POST["a"];  // get internal sensor id from js
    $name = $_POST["n"];  // get value
    require '../php/database.php';

    
    // $sql = "UPDATE probes SET `name`='$name' WHERE `address`='$id'";
    
    $result = $mysqli->query("INSERT IGNORE INTO probes (name, address, plugged, graphVisible, min) VALUES ('".$name."', '".$a."', '1', '1', '100')");
    if ($result === false) {echo "SQL error:".$mysqli->error;}
    
    // if ($mysqli->query($sql) === TRUE) {
        // echo "Widget Visible updated successfully";
    // } else {
        // echo "Error updating Widget Visible: " . $mysqli->error;
    // }
    // //query to get data from the table
    // $result = $mysqli->query("SELECT * FROM ds18b20 WHERE address = '$id'");

    // if (!$result) {  // run query
    //     die($mysqli->error);
    // }
    
    // // echo $result->num_rows;
    // if ($result->num_rows > 0) {  // check if duplicate
    //     // sensor already in database
    // } else {  // insert record as it doesn't exist
    //     $result = $mysqli->query("INSERT IGNORE INTO ds18b20 (address, name, plugged, graphVisible) VALUES ('".$id."', '".$name."', '1', '1')");
    //     if ($result === false) {echo "SQL error:".$mysqli->error;}
    // }


    //close connection
    $mysqli->close();
    $dataWindow = "done";
    //print the data
    print json_encode($dataWindow); 
?>