Ethernet Shield only sends 1/3 of data

I hooked up the ethernet shield to gather data and post to a mysql database. The strange thing is that it works about a 1/3 of the time and I can't figure out where to troubleshoot it.

I programmed it to send data every 10 seconds, and it would end up sending data randomly, usually about twice a minute. Sometimes it would be 40 seconds separation, sometimes 20. It appears to be in 10 second intervals, which makes me think that sometimes the POST is just not going through, other times it is.

Any ideas why this could be? Is this common?

Here's the pertinent part of the code just in case there is something in there:

String data = "temp=" + stempF + "&bright=" + slight;
//  Serial.println(data);
  if (client.connect("www.SERVER.com", 80)) { 
      Serial.println("Connected");
      client.println("POST /data/add.php HTTP/1.1"); 
      client.println("Host: www.SERVER.com"); 
      client.println("Content-Type: application/x-www-form-urlencoded"); 
      client.print("Content-Length: "); 
      client.println(data.length()); 
      client.println(); 
      client.print(data);
      client.println("Connection: close");
      client.println(); 
    } 
  if (client.connected()) { 
      client.stop();	// DISCONNECT FROM THE SERVER
	}

  delay(10000);

Does it connect?

Your connect evaluation isn't correct.

if(client.connect("www.server.com",80) == 1) Serial.println("connected");
else Serial.println("failed");

It usually says "Connected" (accept when I was playing with the server address), so it is executing the code.

Wouldn't if (client.connect("www.SERVER.com", 80)) evaluate to True if connected and False if not connected?

I'll try the change and see what happens.

No. The client.connect will return a negative value if the DNS resolution fails, which also evaluates to true.

I ran it with that change. It still says connected, but posts sporadically.

I changed the post rate to every 5 seconds and it posted the first reading, then waited over a minute to post the second.

I'm looking at the readings in the serial monitor, and it is not delaying the post, it is just dropping most of them.

You should read the response from the server. You may be calling client.stop() too soon.

while(client.connected()) {
  while(client.available()) {
    char ch = client.read();
    Serial.write(ch);
  }
}
client.stop();

It looks good so far! That must've been the problem.

Thanks!