· 5 years ago · Sep 21, 2020, 05:48 PM
1$servername = “localhost";
2// REPLACE with your Database name
3$dbname = “REPLACE_WITH_YOUR_DATABASE_NAME";
4// REPLACE with Database user
5$username = “REPLACE_WITH_YOUR_USERNAME";
6// REPLACE with Database user password
7$password = “REPLACE_WITH_YOUR_PASSWORD";
8// Keep this API Key value to be compatible with the ESP32 code provided in the project page.
9// If you change this value, the ESP32 sketch needs to match
10$api_key_value = “tPmAT5Ab3j7F9";
11$api_key= $sensor = $location = $value1 = $value2 = $value3 = “";
12if ($_SERVER[“REQUEST_METHOD"] == “POST") {
13 $api_key = test_input($_POST[“api_key"]);
14 if($api_key == $api_key_value) {
15 $sensor = test_input($_POST[“sensor"]);
16 $location = test_input($_POST[“location"]);
17 $value1 = test_input($_POST[“value1"]);
18 $value2 = test_input($_POST[“value2"]);
19 $value3 = test_input($_POST[“value3"]);
20 // Create connection
21 $conn = new mysqli($servername, $username, $password, $dbname);
22 // Check connection
23 if ($conn->connect_error) {
24 die(“Connection failed: “ . $conn->connect_error);
25 }
26 $sql = “INSERT INTO SensorData (sensor, location, value1, value2, value3)
27 VALUES (‘" . $sensor . “‘, ‘" . $location . “‘, ‘" . $value1 . “‘, ‘" . $value2 . “‘, ‘" . $value3 . “‘)“;
28 if ($conn->query($sql) === TRUE) {
29 echo “New record created successfully";
30 }
31 else {
32 echo “Error: “ . $sql . “
33“ . $conn->error;
34 }
35 $conn->close();
36 }
37 else {
38 echo “Wrong API Key provided.";
39 }
40}
41else {
42 echo “No data posted with HTTP POST.";
43}
44function test_input($data) {
45 $data = trim($data);
46 $data = stripslashes($data);
47 $data = htmlspecialchars($data);
48 return $data;
49}