Hello everyone, I am facing problems while uploading data to MySQL database.
I have created my own domain name using "infinityfree.net" hosting service.
I have created MySQL database and a PHP code to upload values in the PhpMyAdmin database.
Here is my ESP32 code:
#include <WiFi.h>
const char* ssid = "NetGear";
const char* password = "*123phoenix";
const char* host = "assetmanagement.epizy.com";
String tagID = "123ABC", RTCtime2 = "2020-09-18%2018:58:54";
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
if (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
if (WiFi.status() == WL_CONNECTED) {
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
}
void loop() {
if (WiFi.status() != WL_CONNECT_FAILED) {
WiFiClient client;
const int httpPort = 80;
Serial.println(client.connect(host, httpPort));
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
if (tagID = "123ABC") {
client.print(String("GET http://assetmanagement.epizy.com/connect.php?") +
("tagID=") + tagID + ("&RTCtimestamp=") + RTCtime2 +
" HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: Close\r\n\r\n");
Serial.print(String("GET http://assetmanagement.epizy.com/connect.php?") +
("tagID=") + tagID + ("&RTCtimestamp=") + RTCtime2 +
" HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: Close\r\n\r\n");
Serial.println("Database updated");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 1000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
tagID = "";
}
}
}
}
And Here is my PHP code:
<?php
class gate2{
public $link='';
function __construct($tagID, $RTCtimestamp){
$this->connect();
$this->storeInDB($tagID, $RTCtimestamp);
}
function connect(){
$this->link = mysqli_connect('sql302.epizy.com','epiz_26766463','password') or die('Cannot connect to the DB');
mysqli_select_db($this->link,'epiz_26766463_database') or die('Cannot select the DB');
}
function storeInDB($tagID, $RTCtimestamp){
$query = "insert into gate2 set TagID='".$tagID."' , Timestamp='".$RTCtimestamp."'";
$result = mysqli_query($this->link,$query) or die('Errant query: '.$query);
}
}
if($_GET['tagID'] != '' and $_GET['RTCtimestamp']!=''){
$gate2=new gate2($_GET['tagID'],$_GET['RTCtimestamp']);
}
?>
Serial monitor output:
1
GET http://assetmanagement.epizy.com/connect.php?tagID=123ABC&RTCtimestamp=2020-09-18%2018:58:54 HTTP/1.1
Host: assetmanagement.epizy.com
Connection: Close
Database updated
closing connection
closing connection
My Credentials are:
MySQL host: sql302.epizy.com
MySQL Database: epiz_26766463_database
MySQL Table name: gate2
Username: epiz_26766463
Password: password
Domain name: assetmanagement.epizy.com
Here, the database is not being updated and when I manually put the same URL in search bar, database is updated. Also, This same code works for "localhost". What am I doing wrong here?