So I'm sending two data/variable from my NodeMCU board to a local database server (which is my laptop) via PHP File using HTTP Post. Named "statusauto" and "statusauto2". Code on my NodeMCU:
int statusauto, statusauto2;
http.begin(client, servernameauto);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "api_key=" + apiKeyValueAuto + "&statusauto=" + statusauto + "&statusauto2=" + statusauto2 + "";
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.println(" ");
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(" ");
}
http.end();
this data is sent to file "esp-post-data-auto.php". Here the file execute PHP Form Validation with test_input function similar to here:
W3Schools PHP Form Validation
From what I understand, the variable ($api_key, $statusauto, $statusauto2) is then proceed to file "esp-database-auto.php"
In esp-database-auto.php then statusauto Column will be given data referring to those variables from the previous file (esp-post-data-auto.php). Here is the code:
<?php
$servername = "localhost";
// REPLACE with your Database name
$dbname = "esp_data";
// REPLACE with Database user
$username = "esp";
// REPLACE with Database user password
$password = "esp123";
function insertReading($statusauto, $statusauto2) {
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE device_details SET statusauto=('" . $statusauto . "') WHERE id=1";
$sql2 = "UPDATE device_details SET statusauto=('" . $statusauto2 . "') WHERE id=2";
if ($conn->query($sql) === TRUE) {
return "New record created successfully";
}
else {
return "Error: " . $sql . "<br>" . $conn->error;
}
if ($conn->query($sql2) === TRUE) {
return "New record created successfully";
}
else {
return "Error: " . $sql2 . "<br>" . $conn->error;
}
$conn->close();
}
?>
I believe this code in my PHP file is not right since only 1 row is being updated, the one with variable $sql.
$sql = "UPDATE device_details SET statusauto=('" . $statusauto . "') WHERE id=1";
$sql2 = "UPDATE device_details SET statusauto=('" . $statusauto2 . "') WHERE id=2";
What is the correct way to fix this? Thanks before