I am currently having difficulties sending data from an Arduino using an ethernet shield to a PHP database in MySQL. My problem is that it appears that the server is receiving the data as there is a time stamp when I send it from the Arduino however, the other values it is receiving are incorrect (see images below). The server says the values of "data", and "serial_number" are 0 but they are supposed to be 10 and 24. Along with that the Arduino is saying that the data has been sent successfully. My code for both the server and the Arduino is listed below. Any ideas on how to fix this or what is going on are much appreciated.

enter image description here

Arduino Code:

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

byte mac[] = { 0x00, 0xAB, 0xBA, 0xBC, 0xDD, 0x02 };
EthernetClient client;
String data;
int t;
int h;

void setup() {
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");

  Serial.println("still working here");


void loop() {
t = 10;
h = 24;

data = "temp1=";




  if (client.connect("www.smartwater.x10host.com",80)) {
    client.print("POST /add.php HTTP/1.1\n");
    client.print("Host: www.smartwater.x10host.com\n");
    client.print("Connection: close\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");


  if (client.connected()) {


add.php (Server Code)




    $query = "INSERT INTO `Main` (`data`, `serial_number`) 
        VALUES ('".$temp1."','".$hum1."')"; 


    header("Location: index.php");

connect.php (Server Code)


    function Connection(){

        $connection = mysql_connect($server, $user, $pass);

        if (!$connection) {
            die('MySQL ERROR: ' . mysql_error());

        mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );

        return $connection;

index.php (Server Code)




    $result=mysql_query("SELECT * FROM `Main` ORDER BY `timeStamp` DESC",$link);


   <table border="1" cellspacing="1" cellpadding="1">
            <td>&nbsp;Serial Number&nbsp;</td>


             while($row = mysql_fetch_array($result)) {
                printf("<tr><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>", 
                   $row["timeStamp"], $row["data"], $row["serial_number"]);


3 Answers 3


In your Arduino code, the function call that send the real data within the HTTP-POST call, is this:


When executed, the content of the data variable is sent to the server.

Supposing data is a String object then data.concat() is this function, allowing "... String, string, char, byte, int, unsigned int, long, unsigned long, float, double..." as argument.

So, after the execution, data should contain:


and such content is exactly what is passed, via POST to the /add.php script.

Basically, you're passing two variables, called "temp1" and "hum1". Please keep note of those variable names!

Now let's switch the the PHP script, where you have:


Above two statements, asks PHP to retrieve, via the $_POST function, the content of the two variables data and serial_number.

As such variables are undefined (the variables you passed were temp1 and hum1), PHP consider them like 0 and, as such, assign 0 to $temp1 and $hum1.

I bet that to solve your problem you only need to change above PHP with:


BTW: please note that what is inside the $_POST call is totally unrelated with the names of the variables you have on the left of the = sign: they are really different things!


Data = 10 is not the characters making 1 and 0 but an ascii character with value 10, your php script doesn't understand that. Try using itoa function to make character array 1 and 0 for your php to understand.


You get a timestamp from running the php file. That doest mean that there is a number in $data or $serialnmber. What happens if you fill in a value in your php file instead of the variables $data and serialnumber. If the dbase gets filled, you know that than your variables were empty. If they arent filled something is wrong in your php coding

Not the answer you're looking for? Browse other questions tagged or ask your own question.