0% found this document useful (0 votes)
6 views

PHP Code

This PHP code receives sensor data posted from an ESP device via HTTP POST, validates an API key, cleans the input, and inserts the data into a MySQL database table. It establishes a database connection, prepares an SQL statement to insert the sensor and location data fields, executes the query, and returns a success or error message.

Uploaded by

bastoutaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PHP Code

This PHP code receives sensor data posted from an ESP device via HTTP POST, validates an API key, cleans the input, and inserts the data into a MySQL database table. It establishes a database connection, prepares an SQL statement to insert the sensor and location data fields, executes the query, and returns a success or error message.

Uploaded by

bastoutaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Post Data PHP Code

<?php
$servername = "localhost";
$dbname = "id13697105_esp_data";
$username = "id13697105_esp_board";
$password = "4TOc7~@ba9Ie]Xs*";

$api_key_value = "4TOc7~@b";
$api_key= $SensorData = $LocationData = $value1 = $value2 = $value3 = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$api_key = test_input($_POST["api_key"]);
if($api_key == $api_key_value) {
$SensorData = test_input($_POST["SensorData"]);
$LocationData = test_input($_POST["LocationData"]);
$value1 = test_input($_POST["value1"]);
$value2 = test_input($_POST["value2"]);
$value3 = test_input($_POST["value3"]);

$conn = new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO ESPData (SensorData, LocationData, value1,


value2, value3)
VALUES ('" . $SensorData . "', '" . $LocationData . "', '" . $value1
. "', '" . $value2 . "', '" . $value3 . "')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
else {
echo "Wrong API Key";
}

}
else {
echo "No data posted HTTP POST.";
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

You might also like