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.