PHP script uploading help please

I am attempting to upload data via a PHP script. The below code outputs the url for debugging, and when putting that url directly into the browser it updates the database successfully, so the problem must be in the connection. But I have no idea how to debug a connection issue. If anyone can see the problem in this code that would be the best help, if not if you could point me in a direction of lines to add to produce output that would tell me where the problem is that would be helpful as well.

(I am not really uploading to www.example.com, just don't want to put my actual PHP script location out there for all to see.)

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

byte mac[] = {  0x90, 0xA2, 0xDA, 0x0D, 0x25, 0x00 };  
char server[] = "www.example.com";  
byte ip[] = { 192,168,1,127 };
EthernetClient client;
String senddata = ""; 
char inChar;
int siteID = 49;
String inputString = "";         
String transmitterid = "0";
String sensorid = "0";
String sensorcondition = "0";

void setup() {
  Serial.begin(9600);  
  Serial.println("Attempting to get an IP address using DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("failed to get an IP address using DHCP, trying manually");
    Ethernet.begin(mac, ip);
  }
  
  Serial.print("My address: ");
  Serial.println(Ethernet.localIP());
  delay(1000);
  Serial.println("Waiting for input...");
}

void loop() {
  
}

void serialEvent() {
  static int i = 0;
  while (Serial.available()) {
    
    char inChar = (char)Serial.read();
    
    if (inChar != ','){
    inputString += inChar;
    }
    else {
      i++;
      switch(i){
         case 1:
            sensorid = inputString;
            inputString = "";
         break;
         case 2:
            sensorcondition = inputString;
            inputString = "";
         break;
         case 3:
            transmitterid = inputString;
            inputString = "";
         break; 
      }
    }
    
    if (inChar == '\n') {
      
      Serial.println(sensorid);
      Serial.println(sensorcondition);
      Serial.println(transmitterid);
      senddata = "GET /updatelog.php?Sensor=";
      senddata += sensorid; 
      senddata += "&Condition=";
      senddata += sensorcondition;
      senddata += "&transmitter=";
      senddata += transmitterid;
      senddata += "&siteID=";
      senddata += siteID;
      senddata += " HTTP/1.0";
      if (client.connect(server, 80)) {
        client.println(senddata);
        Serial.print(senddata);
      }
      else{
        Serial.println("connection failed");
        Serial.print(senddata); 
      }                
      delay(2000); //let the server process this sensor data before sending more
      client.stop();
    } 
  }
}
String senddata = ""; 
String inputString = "";         
String transmitterid = "0";
String sensorid = "0";
String sensorcondition = "0";

You need to lose all of these until the problems with the free() function are fixed. Even then, you should learn how to deal with char arrays, rather than Strings.

  delay(1000);
  Serial.println("Waiting for input...");

Wait a while, doing NOTHING, then say you're waiting. What possible reason do you have for that delay()?

The Arduino is getting data from the serial port and sending it to a server? Why don't you just have the PC send the data to the server, and turn the Arduino off?

What are you seeing on the serial port? What do the logs on www.example.com say about the (failed) connection attempt?

What does updatelog.php say about the input data? You do have debug statements in that script, don't you?

Is this your server? Or do you lease space on a commercial server. If it is the latter, they may use virtual hosting. And I did not see a double CR/LF in your request, so I added that to the Host line.

      if (client.connect(server, 80)) {
        client.println(senddata);
        client.println("Host: www.example.com\r\n");
        Serial.print(senddata);
      }
      else{
        Serial.println("connection failed");
        Serial.print(senddata); 
      }

I think you need to provide the Host and perhaps other headers as well.
This may be helpful:

@PaulS - Thanks for responding!

You need to lose all of these until the problems with the free() function are fixed. Even then, you should learn how to deal with char arrays, rather than Strings.

Thanks for the tip, but I have tried that. inputString += inChar; This line causes errors when I try to use character arrays. I found the String datatype and it works just fine. Can you explain or point me to an article that would further explain the problem with using the String datatype please?

Wait a while, doing NOTHING, then say you're waiting. What possible reason do you have for that delay()?

Good question. It was there for a reason at some point, but through all the changes I've made; it is now obsolete, and just didn't get removed.

The Arduino is getting data from the serial port and sending it to a server? Why don't you just have the PC send the data to the server, and turn the Arduino off?

Another good question, I didn't fully explain my project. This was the second of two separate questions I asked and I explained the project in the first and forgot to do so here. The arduino is eventually going to receive the data from another arduino via xbee radio, and then upload it. I need the computer to be out of the equation.

What are you seeing on the serial port?

my appologies again, should have included the serial output.

Attempting to get an IP address using DHCP:
My address: 192.168.1.120
Waiting for input...

then I input 5,6,7, and get

5
6
7
GET /updatelog.php?Sensor=5&Condition=6&transmitter=7&siteID=49 HTTP/1.0

What do the logs on www.example.com say about the (failed) connection attempt?

What does updatelog.php say about the input data? You do have debug statements in that script, don't you?

They say nothing, they are not receiving the connection attempt.

@SurferTim - Thanks for helping!
I tried adding the host as you suggested with no change in behavior. Good catch though, I did have that at one time and it somehow got misplaced. :blush:

This line causes errors when I try to use character arrays.

Well, of course it does. The char data type does not overload the += operator. That is something that only classes can do.

You need to have an index into the array, and update that index as you add data to the array.

I found the String datatype and it works just fine.

You mean that it hasn't burned you. Yet.

Can you explain or point me to an article that would further explain the problem with using the String datatype please?

A little time spent on the forum would have you overwhelmed with issues related to the String class and it's use/misuse/related code issues. You can search as well as I can.

@utwg - Thanks for the information! I looked at the link you provided, and although it gives a lot of good information I'm not sure how to apply it to my situation. Should I be adding the "Host: www.example.com\r\n" to the senddata variable and sending it all at once, or is it okay to send it in separate client.println statements. Perhaps someone can provide a working GET statement in it's entirety so I can see what the format is and apply that here?

is it okay to send it in separate client.println statements.

Yes. The server can't tell whether the data was sent all at once, or one record at a time.

Sending one letter at a time will cause problems, since each send creates one or more packets. But, sending each record one at a time is generally better than dealing with the memory use/misuse that comes from trying to send the whole GET request in one packet.

You might want to wait for the server response. It may get your client.stop() before it sends anything. BTW, if the serial monitor is displaying the GET line, you are connected.

      if (client.connect(server, 80)) {
        client.println(senddata);
        Serial.print(senddata);
      }
      else{
        Serial.println("connection failed");
        Serial.print(senddata); 
      }                

      // read the response until the server closes the connection...
      while(client.connected()) {
         while(client.available()) {
            Serial.write(client.read());
         }
      }
      // ...then close your end
      client.stop();
      Serial.println("\r\nConnection closed");

edit: Insure when you send that request, there is a double CR/LF at the end of youe GET request. The println is one, and the "\r\n" is the other.
client.println("Host: www.example.com\r\n");

while(client.connected()) {

while(client.available()) {
            Serial.write(client.read());
        }
      }

Exactly what I needed! Thanks! Viewing the output of the webpage allowed me to see that I had an error in my SQL statement. It worked yesterday through the browser, but I made some changes to the PHP trying to discover the reason the arduino wasn't working, and forgot to change it back. Works beautifully now.

Note to self: change one thing at a time.

@PaulS - Thanks for the heads up on strings, since these are relatively small values I have reserved some memory for them, from what I've read that should avoid any issues. I will however heed your advice and learn to use character arrays sometime in the future when I'm not frantically trying to make something work. Thanks for your time one and all!

BTW, it seems the origional problem was the lack of a HOST being sent. And then in the interim of that not working, I made some other changes that broke it elsewhere. But for anyone else with the same issue, I believe my problem was the lack of the HOST, and the lack of the proper line ending.

And the lack of an ability to multitask, but that's another story.