Send RFID Reading to phpMyAdmin

I can't send my arduino data to phpmyadmin's table, here's my code. Can anyone help me?

//Connect RFID and Send it to phpMyAdmin
#include <Ethernet.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 53
#define RST_PIN 49

MFRC522 rfid(SS_PIN, RST_PIN);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };                 // Setting MAC Address

char server[] = "192.168.37.68";                                     // IP address Komputer
IPAddress ip(192,168,37,177);                                        // IP address Ethernet Shield
EthernetClient client;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  Serial.println("Please Wait");
  rfid.PCD_Init();
    if (Ethernet.begin(mac) == 0) {
    Serial.println("Tap Your Card");
    Ethernet.begin(mac, ip);
    }
    else{
    Serial.println("Failed to configure Ethernet using DHCP");
    }
}

void loop() {
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    Serial.print("UID tag :");
    String content = "";
    byte letter;
    for (byte i = 0; i < rfid.uid.size; i++) {
      Serial.print(rfid.uid.uidByte[i], HEX);
      content.concat(String(rfid.uid.uidByte[i], HEX));
    }
  content.toUpperCase();
  delay(3000);
  Sending_To_phpmyadmindatabase(content.substring(1));               // CONNECTING WITH MYSQL
  }
}

void Sending_To_phpmyadmindatabase(String UID)
{
  if (client.connect(server, 80)) {                                  
    Serial.println();
    Serial.println("Connected");
    Serial.print("GET /testcode/3Acc_CatatFix.php?");                // Make a HTTP request    
    Serial.print("UID=");
    Serial.print(UID);                                               Pembacaan Digit pertama UID
    Serial.println();

    client.print("GET /testcode/3Acc_CatatFix.php?UID=");                // Make a HTTP request    
    client.print(UID);                                               
    client.println();
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: localhost");                               
    client.println("Connection: close");
    client.println();
  } 
  else {
    Serial.println();
    Serial.println("Connection Failed");
    }
  }

    Serial.print(UID);                                               Pembacaan Digit pertama UID

Is this the line that gives an error? Were those words intended to be a comment? If so, you need to put // before them.

If your code will not compile/verify, you should always post the complete set of error messages, using code tags.

the program can be compiled and uploaded normally, but the rfid card can't be displayed in the phpmyadmin. Should i send the php program too?

<?php
// Connect to MySQL database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rfid";
$conn = new mysqli($servername, $username, $password, $dbname);
	
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Table 1
	$sql = "SELECT * FROM personil_rfid WHERE UID = '".$_GET['UID']."'";
	$result = $conn->query($sql);
	$row = $result -> fetch_assoc();
	if ($row["UID"] == $_GET['UID']) {
	//Table 2
		$sql2 = "SELECT * FROM access_rfid WHERE Access = 'Allow' and UID = '".$_GET['UID']."'";
		$result2 = $conn->query($sql2);
	
//Jika True pada Table 1 & 2 (Output = 'Access Granted')
	if ($result2->num_rows > 0) {
		$row2 = $result2->fetch_assoc();
		$Nama = $row2['Nama'];
		echo " Access Granted for <br>";
		echo $Nama;
} 
//Jika True pada Table 1 saja (Output = 'Access Denied')
	else {
		$sql3 = "SELECT * FROM access_rfid WHERE Access = 'Denied' and UID = '".$_GET['UID']."'";
		$result3 = $conn->query($sql3);
		$row3 = $result3->fetch_assoc();
		$Nama = $row3['Nama'];
		echo " Access Denied for <br>";
		echo $Nama;
	}	
} 
//Jika False (Output = 'UID Not Found')
	else {
    	echo "UID Not Found";
}
// Close database connection
$conn->close();

class rfid{
	public $link='';
	function __construct($Nama, $UID){
	 	$this->connect();
	 	$this->storeInDB($Nama, $UID);
	}
	
	function connect(){
	 	$this->link = mysqli_connect('localhost','root','') or die('Cannot connect to the DB');
	 	mysqli_select_db($this->link,'rfid') or die('Cannot select the DB');
	}
 	function storeInDB($UID){
  		$kondisi = "SELECT Access FROM access_rfid WHERE UID = '".$_GET['UID']."'";
		$result = mysqli_query($this->link, $kondisi) or die('Errant query: '.$kondisi);
		$row = mysqli_fetch_assoc($result);
		$access = $row['Access'];
		$kondisi = "SELECT Nama FROM personil_rfid WHERE UID = '".$_GET['UID']."'";
		$result2 = mysqli_query($this->link, $kondisi) or die('Errant query: '.$kondisi);
		$row = mysqli_fetch_assoc($result2);
		$Nama = $row['Nama'];
		$query = "INSERT INTO data_rfid (UID, Kondisi, Nama) VALUES ('".$_GET['UID']."', '".$access."', '".$Nama."')";  
		$result = mysqli_query($this->link, $query) or die('Errant query: '.$query);
		}
}

if($_GET['UID'] != ''){
	$rfid=new rfid($Nama, $_GET['UID']);
}

?>

And yes, i made this program to receive those uid number and send it to phpmyadmin.

Hey everyone, something wrong with my program.
My program can be compile and uploaded fine (No Error Message) , but my arduino seem can't send the rfid reading (UID Number) to php program in order to write it in phpmyadmin table.
But, when i test the php program with manual url (localhost/testcode/connectdb.php?UID=A1A2A3A4A5) the program seems work as it should be (The UID got crosscheck and display in "data_rfid".)
And that's why i assume that something wrong with my arduino program.

This is my arduino Program

#include <Ethernet.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 53
#define RST_PIN 49

MFRC522 rfid(SS_PIN, RST_PIN);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };                 // Setting MAC Address

char server[] = "192.168.37.68";                                     // IP address Komputer
IPAddress ip(192,168,37,177);                                        // IP address Ethernet Shield
EthernetClient client;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  Serial.println("Please Wait");
  rfid.PCD_Init();
    if (Ethernet.begin(mac) == 0) {
    Serial.println("Tap Your Card");
    Ethernet.begin(mac, ip);
    }
    else{
    Serial.println("Failed to configure Ethernet using DHCP");
    }
}

void loop() {
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    Serial.print("UID tag :");
    String content = "";
    byte letter;
    for (byte i = 0; i < rfid.uid.size; i++) {
      Serial.print(rfid.uid.uidByte[i], HEX);
      content.concat(String(rfid.uid.uidByte[i], HEX));
    }
  content.toUpperCase();
  delay(3000);
  Sending_To_phpmyadmindatabase(content.substring(1));               // CONNECTING WITH MYSQL
  }
}

void Sending_To_phpmyadmindatabase(String UID)
{
  if (client.connect(server, 80)) {                                  
    client.print("GET /testcode/connectdb.php?UID=");                // Make a HTTP request    
    client.print(UID);                                               // Pembacaan Digit pertama UID
    client.println();
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: localhost");                               // IP Adress Komputer
    client.println("Connection: close");
    client.println();
  } 
  else {
    Serial.println();
    Serial.println("Connection Failed");
    }
  }

And here is my php program

<?php
// Connect to MySQL database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rfid";
$conn = new mysqli($servername, $username, $password, $dbname);
	
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Table 1
	$sql = "SELECT * FROM personil_rfid WHERE UID = '".$_GET['UID']."'";
	$result = $conn->query($sql);
	$row = $result -> fetch_assoc();
	if ($row["UID"] == $_GET['UID']) {
	//Table 2
		$sql2 = "SELECT * FROM access_rfid WHERE Access = 'Allow' and UID = '".$_GET['UID']."'";
		$result2 = $conn->query($sql2);
	
//Jika True pada Table 1 & 2 (Output = 'Access Granted')
	if ($result2->num_rows > 0) {
		$row2 = $result2->fetch_assoc();
		$Nama = $row2['Nama'];
		echo " Access Granted for <br>";
		echo $Nama;
} 
//Jika True pada Table 1 saja (Output = 'Access Denied')
	else {
		$sql3 = "SELECT * FROM access_rfid WHERE Access = 'Denied' and UID = '".$_GET['UID']."'";
		$result3 = $conn->query($sql3);
		$row3 = $result3->fetch_assoc();
		$Nama = $row3['Nama'];
		echo " Access Denied for <br>";
		echo $Nama;
	}	
} 
//Jika False (Output = 'UID Not Found')
	else {
    	echo "UID Not Found";
}
// Close database connection
$conn->close();

class rfid{
	public $link='';
	function __construct($Nama, $UID){
	 	$this->connect();
	 	$this->storeInDB($Nama, $UID);
	}
	
	function connect(){
	 	$this->link = mysqli_connect('localhost','root','') or die('Cannot connect to the DB');
	 	mysqli_select_db($this->link,'rfid') or die('Cannot select the DB');
	}
 	function storeInDB($UID){
  		$kondisi = "SELECT Access FROM access_rfid WHERE UID = '".$_GET['UID']."'";
		$result = mysqli_query($this->link, $kondisi) or die('Errant query: '.$kondisi);
		$row = mysqli_fetch_assoc($result);
		$access = $row['Access'];
		$kondisi = "SELECT Nama FROM personil_rfid WHERE UID = '".$_GET['UID']."'";
		$result2 = mysqli_query($this->link, $kondisi) or die('Errant query: '.$kondisi);
		$row = mysqli_fetch_assoc($result2);
		$Nama = $row['Nama'];
		$query = "INSERT INTO data_rfid (UID, Kondisi, Nama) VALUES ('".$_GET['UID']."', '".$access."', '".$Nama."')";  
		$result = mysqli_query($this->link, $query) or die('Errant query: '.$query);
		}
}

if($_GET['UID'] != ''){
	$rfid=new rfid($Nama, $_GET['UID']);
}

?>

I would love to have some help :slight_smile:

I just have to ask... it's 2023 why PHP?

add a Serial.println(UID); at the begin of the function so you can see, what you handover to the function.

furthermore you are currently splitting your get request with a println, that doesn't look correct.

Try

void Sending_To_phpmyadmindatabase(String UID)
{
  Serial.println(UID);
  if (client.connect(server, 80)) {                                  
    client.print("GET /testcode/connectdb.php?UID=");                // Make a HTTP request    
    client.print(UID);                                               // Pembacaan Digit pertama UID
    //client.println();  // why???
    client.println(" HTTP/1.1"); 
    //client.println();
    client.println("Host: localhost");    // I don't believe that localhost is right in this context, should be the server you are sending to...

p.s. sanitize and harden your PHP script against malformed parameters...

alright, i'll do that soon.
Thanks :smiley:

Well, i don't really understand about all this stuff. i'm still new at this kind of stuff.
Do you have any suggestion to make it easier?

No, it can't, because of the problem I described in post #2.

ahh yes i already put //, when i copy the code it got deleted by mistake. so yeah the program can be compile normally. but it still can't send the uid to phpmyadmin.

Have you successfully tested the URL from your browser?

By this I mean enter

http://192.168.37.68/testcode/3Acc_CatatFix.php?UID=XYX

What response do you get and is the database updated?

By the way, the database is not phpMyAdmin! It is probably mySQL. phpMyAdmin is a browser-based app for administering mySQL and similar databases (creating tables, viewing and amending data etc). The Arduino is not connecting with phpMyAdmin, it is connecting with your PHP script, which connects to the mySQL database.

I have merged your cross-posts @bnetchalvit.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Ah i see, yeah i meant mySQL Database.
Yes, i've successfully tested the url with my browser and it's updated the database as well.
So yeah, my problem is...when i want to send my rfid data to the database, either my arduino can't send the rfid reading to PHP Script and connect it with mySQL Database
or
My PHP Script can't receive the rfid reading and send it to mySQL Database.

show your actual Arduino Code

This is my newest arduino code....

#include <Ethernet.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 53
#define RST_PIN 49

MFRC522 rfid(SS_PIN, RST_PIN);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };                

char server[] = "192.168.37.68";                                     
IPAddress ip(192,168,37,177);                                       
EthernetClient client;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  Serial.println("Please Wait");
  rfid.PCD_Init();
    if (Ethernet.begin(mac) == 0) {
    Serial.println("Tap Your Card");
    Ethernet.begin(mac, ip);
    }
    else{
    Serial.println("Failed to configure Ethernet using DHCP");
    }
}

String content = "";

void loop() {

  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    Serial.print("UID tag :");
    content = "";
    byte letter;
    for (byte i = 0; i < rfid.uid.size; i++) {
      Serial.print(rfid.uid.uidByte[i], HEX);
      content.concat(String(rfid.uid.uidByte[i], HEX));
    }
  content.toUpperCase();
  delay(3000);
  Sending_To_phpmyadmindatabase(content.substring(1));             
  }
}

void Sending_To_phpmyadmindatabase(String UID)
{
  if (client.connect(server, 80)) {   
    client.print("GET /testcode/MixProgram.php?UID=");               
    client.print(content);
    client.print("HTTP/1.1");
    client.println("Host: xxxx");                                   
    client.println("Connection: close");
    client.println();
  } 
  else {
    Serial.println();
    Serial.println("Connection Failed");
    }
  }

Also i want to ask,

    client.println("Host: xxxx");      

what should i fill the xxxx part with?

when the server is in your local lan on a dedicated device you don't need it.

But you haven't followed my proposal exactly. you are missing a blank

  if (client.connect(server, 80)) {   
    client.print("GET /testcode/MixProgram.php?UID=");               
    client.print(content);
    client.println(" HTTP/1.1"); // blank was missing and line end
   // client.println("Host: xxxx");                                   
    client.println("Connection: close");
    client.println();
  } 

noted... and what exactly you mean with "blank was missing"??
Did i messed up my code by removing some important part of the code?

compare that line with your code.
character by character.
What is the difference?

  if (client.connect(server, 80)) {   
    client.print("GET /testcode/MixProgram.php?UID=");               
    client.print(content);
    //client.println();
    client.print("HTTP/1.1");
    //client.println();                           
    client.println("Connection: close");
    client.println();

I won't lie to you but the only thing i notice, is the

client.println();

i thought u told me to give it "//" no?