· 6 years ago · Feb 07, 2020, 04:34 PM
1<?php
2
3// For the Database :
4$servername = "localhost";
5$dbname = "StationMeteo";
6$username = "root";
7$password = "burobot2019";
8
9//the inviolable security
10$api_key_value = "neil";
11
12$api_key= $humidity = $temp_h = $pressure = $light_lvl = $winddir = $windspeedmph = $windspdmph_avg5m = $winddir_avg5m = $rainin = $rainValue = "";
13
14echo $_POST;
15
16if ($_SERVER["REQUEST_METHOD"] == "POST") {
17 $api_key = test_input($_POST["api_key"]);
18 if($api_key == $api_key_value) {
19 $humidity = test_input($_POST["humidity"]);
20 $temp_h = test_input($_POST["temp_h"]);
21 $pressure = test_input($_POST["pressure"]);
22 $light_lvl = test_input($_POST["light_lvl"]);
23 $winddir = test_input($_POST["winddir"]);
24 $windspeedmph = test_input($_POST["windspeedmph"]);
25 $windspdmph_avg5m = test_input($_POST["windspdmph_avg5m"]);
26 $winddir_avg5m = test_input($_POST["winddir_avg5m"]);
27 $rainin = test_input($_POST["rainin"]);
28 $rainValue = test_input($_POST["rainValue"]);
29
30 // Create connection
31 $conn = new mysqli($servername, $username, $password, $dbname);
32 // Check connection
33 if ($conn->connect_error) {
34 die("Connection failed: " . $conn->connect_error);
35 }
36
37 $sql = "INSERT INTO SensorData (humidity, temp_h, pressure, light_lvl, winddir, windspeedmph, windspdmph_avg5m, winddir_avg5m, rainin, rainValue) VALUES ('" . $humidity . "', '" . $temp_h . "', '" . $pressure . "', '" . $light_lvl . "', '" . $winddir . "', '" . $windspeedmph . "', '" . $windspdmph_avg5m . "', '" . $winddir_avg5m . "', '" . $rainin . "', '" . $rainValue . "')";
38
39 if ($conn->query($sql) === TRUE) {
40 echo "New record created successfully";
41 }
42 else {
43 echo "Error: " . $sql . "<br>" . $conn->error;
44 }
45
46 $conn->close();
47 }
48 else {
49 echo "Wrong API Key provided.";
50 }
51
52}
53else {
54 echo "No data posted with HTTP POST.";
55}
56
57function test_input($data) {
58 $data = trim($data);
59 $data = stripslashes($data);
60 $data = htmlspecialchars($data);
61 return $data;
62}