Arduino Post to Mysql

Ok, I officially need help. If anyone is able to help me figure this out, I will be so appreciative. I have attempted to troubleshoot this on my own, but realized that you all have done this before and could easily spot my mistake. Below is my complete request.

Equipment: Arduino UNO, Arduino Ethernet Shield.

Goal: send HTTP request to php file, update database where username is derived from $_SESSION[“username”],

(This may be the issue, but I am 60% sure that username is successfully being called and the php script is able to insert the GET request where username = $_SESSION[“username”] on the server side because I don’t see the error in the response from the Serial Monitor, however, I did see it when I didn’t set the variable up correctly). However, I am aware that I may never see an error message as the error message is set within the conditional statement (isset($_REQEUEST[“arduino”])), that’s why I’m only 60% sure.

What I’ve accomplished: HTTP request sent, response code = 200. My php script/page functions appropriately by updating the database when I enter enter a value directly into the URL i.e. get.php?/arduino=working, will update the database).

Out of curiosity: I’ve seen 2 types of HTTP request while searching, one type of script codes the key value pair directly into the HTTP header for example (GET /get.php?arduino=working HTTP/1.1). The other type of request places the key value pair into the appropriate message body of the HTTP request. I’ve tried both and I am still stuck. I should say that when I code the appropriate way (key pair in the message body) I receive a response from the server, whereas I do not when I compile the entire request in the url.

Project Goal: I will scan the pins using PIND to detect whether a pin value is set to HIGH, when that value is set to HIGH, I will post that pin’s information to the database.

Arduino

#include <SPI.h>
#include <Ethernet.h>
#define pinTag PIND
String Select;


// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "mysite.000webhostapp.com";    

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 1);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  
  Serial.begin(115200);
      Serial.begin(9600);
      DDRD = B00000000; //2-7 are inputs
      while (!Serial)
          {; // wait for serial port to connect. Needed for native USB port only
          }
          Serial.println();
          connection();  
          post();
}

void connection()
    {
    // start the Ethernet connection:
    if (Ethernet.begin(mac) == 0)
        {
        Serial.println("Failed to configure Ethernet using DHCP");
        // try to congifure using IP address instead of DHCP:
        Ethernet.begin(mac, ip);
        }
          // give the Ethernet shield a second to initialize:
          delay(1000);
          Serial.println("connecting...");
    }

void post()
{
  Serial.println("connecting...");
  String PostData="arduino=";
    PostData=PostData+"Selected";
   
  Serial.println(PostData);
  if (client.connect(server, 80)) {
    Serial.println("connected"); 
            client.print("GET /get.php");
            client.print(PostData);
            client.println(" HTTP/1.1");
            client.println("Host:  mysite.000webhostapp.com");
            client.println("User-Agent: Arduino/1.0");
            client.println("Connection: close");
            //client.println("Content-Type: application/x-www-form-urlencoded;");
            //client.print("Content-Length: ");
            //client.println(PostData.length());
            client.println();
            //client.println(PostData);
            
    } else {
    Serial.println("connection failed");
  }
  //delay(1000);
}


void clientReadData()
    {
        //if incoming bytes available, read/print them
        if (client.available()) 
           {
            char c = client.read();
            Serial.print(c);
           }
    }

PHP

<?php 

session_start();
   	include("connect.php");
   	include("js/scripts.php");
   	
   	$conn = Connection();
  
while (isset($_REQUEST["arduino"])) 
{ 	
	$username = $_SESSION["username"];  
	$arduino = $_REQUEST["arduino"];
					 	$query = "UPDATE ranking SET 
								`arduino` = '".$_REQUEST["arduino"]."'
						WHERE username= '".$username."'  ";  
								
								
	mysqli_query($conn,$query)or die(mysqli_error($conn));
	
	//header("Location: database.php");
}
 mysqli_error($conn);
 
?>

TLDR;
I am attempting to update a mysql database with an Arduino UNO.

Thank you all very much for your help.

Where is username defined?
you can echo values and have the arduino read it and you can print it to serial to debug.

Also, your data format is for POST data yet you're telling the server something else
client.print("GET /get.php");

Username defined is in session.php.

I commented out the other way (which I thought was the standard way), of sending an HTTP request.

This is my current HTTP request

GET /get.php HTTP/1.1
Host:  mysite.000webhostapp.com
User-Agent: Arduino/1.0
Connection: close
Content-Type: application/x-www-form-urlencoded;
Content-Length: 20

arduino=Selected

In regards to proper formatting, I'm not sure how I should change it. From what I see online, the GET method has the entire URL completed with the key value pairs.

I've commented out what I believe is the POST method. would you mind letting me know where I went wrong?

Ok, so you don't understand the difference between GET and POST.

Try
POST / get.php http/1.1

I see, with POST requests the values are sent in the body, thank you. I’ve updated the code, for some reason the database hasn’t updated.

To remove confusion, the PHP file now refers to a known username in the database and not a SESSION username. Below is my new HTTP Request

POST /get.php HTTP/1.1
Host:  mysite.000webhostapp.com
User-Agent: Arduino/1.0
Connection: close
Content-Type: application/x-www-form-urlencoded;
Content-Length: 16

arduino=Selected

I think I've gotten it, it wasn't reading the session username. And the formatting was not correct for my http request. Thank you. Now to figure out how to retrieve a cookie and use it in the header

If anyone else sees this and would like to help out, please feel free.

Thank you mistergreen.

Dude, you state your equipment and goals and all that, which is great, but you don't actually say what your problem is.

What is your setup doing (or not doing) that makes you think there is something going wrong with it?

I mean,

What I've accomplished: HTTP request sent, response code = 200.

if you are getting a 200 back, then communication from the arduino side is working just fine.

If the arduino gets a properly-constructed 200 in return, AND when you type the url directly into a web browser it works, then i would suspect that either your arduino isn't using the exact same http GET that you are typing into your browser. Or router configuration issues.

Add some logging to your php. Do you get logging when you call it from a web browser? Do you get any logging when you call it from the arduino? Do you get any logging when you call it from a different machine on the same network (eg: type it into your tablet)?

Maybe your arduino is connecting to something else that is serving up /get.php . Do you have another web server running? Did you restart your web server and fail to kill a previous instance of it? Try changing your php to return a 500 response code. Does the arduino still say that it's getting a 200? If so, then the arduino isn't talking to what you think it's talking to.

Hey PaulMurrayCbr, thanks for responding. I made the mistake of attempting to insert the data where the username matched the active SESSION username. When that failed I made more errors to my arduino code while the main issue went unchecked.

The database updates successfully now that I'm not trying to UPDATE  tablename SET arduino = $_REQUEST["arduino"] WHERE username = $_SESSION["username"]

Now I have to find a way to get the SESSION username and put that in the header so I can update the database where the username is found.

Do you have any experience with that? If so, any input would definitely be appreciated.

In the link I gave you, it shows how a cookie is formatted.

Solicitó ayuda, tengo un sensor de humedad en el arduino ethernet y el dato lo quiero enviar a mysql, el mysql lo tengo instalado en la tarjeta galileo y es ahí donde quiero que llegué ese dato, gracias.