· 6 years ago · Sep 03, 2019, 04:41 PM
1<?php
2/*
3 Rui Santos
4 Complete project details at https://RandomNerdTutorials.com
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files.
8
9 The above copyright notice and this permission notice shall be included in all
10 copies or substantial portions of the Software.
11*/
12
13$servername = "localhost";
14
15// REPLACE with your Database name
16$dbname = "epiz_24424467_temphumid";
17// REPLACE with Database user
18$username = "epiz_24424467";
19// REPLACE with Database user password
20$password = "wNfERlRt1bJj";
21
22// Keep this API Key value to be compatible with the ESP32 code provided in the project page. If you change this value, the ESP32 sketch needs to match
23$api_key_value = "tPmAT5Ab3j7F9";
24
25$api_key = $Temperature = $Humidity = "";
26
27if ($_SERVER["REQUEST_METHOD"] == "POST") {
28 $api_key = test_input($_POST["api_key"]);
29 if($api_key == $api_key_value) {
30 $Temperature = test_input($_POST["Temperature"]);
31 $Humidity = test_input($_POST["Humidity"]);
32
33
34 // Create connection
35 $conn = new mysqli($servername, $username, $password, $dbname);
36 // Check connection
37 if ($conn->connect_error) {
38 die("Connection failed: " . $conn->connect_error);
39 }
40
41 $sql = "INSERT INTO (Temperature, Humidity)
42 VALUES ('" . $Temperature . "', '" . $Humidity . "')";
43
44 if ($conn->query($sql) === TRUE) {
45 echo "New record created successfully";
46 }
47 else {
48 echo "Error: " . $sql . "<br>" . $conn->error;
49 }
50
51 $conn->close();
52 }
53 else {
54 echo "Wrong API Key provided.";
55 }
56
57}
58else {
59 echo "No data posted with HTTP POST.";
60}
61
62function test_input($data) {
63 $data = trim($data);
64 $data = stripslashes($data);
65 $data = htmlspecialchars($data);
66 return $data;
67}