Error 400 Bad Request

So I'm trying to send data from ESP8266 to a local MSSQL database. I'm using XAMPP and I have a working php script that inserts data into the database, but I can't get the arduino code to send anything.

This is my arduino code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

void connecttowifi();
void sendDataToDB();

String Data ="TEST"; //test data

char ssid[] = "ssid";  //wifi ssid
char password[] = "password"; //wifi password 
WiFiClient client;
char server[] = "192.168.1.6"; 

void setup() 
{
    Serial.begin(115200);
    delay(10); 
    connecttowifi();

}


void connecttowifi() 
{
    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    delay(1000);
}

void sendDataToDB(){
  if(client.connect(server, 80))
        {  
            Serial.println("Connected...");
            Serial.println("Making HTTP request...");
    
            client.print("GET /insert.php?data="); //php code for database
            client.print(Data); //data
            client.print(" HTTP/1.1");
            client.print("Host: ");
            client.println(server);
            client.println("Connection: close");
            client.println(); 

            Serial.println("HTTP request sent");
            delay(3000);
            if(client.available())
            {
                Serial.println("HTTP request received");
      
                while(client.available())
                {
                    char c = client.read();
                    Serial.print(c);
                }
            }
            else
            {
                Serial.println("No response received");
            }
            client.stop();
        }
        else
        {
            Serial.println("connection failure");
        }
        
}

void loop() {
        sendDataToDB();
        Serial.print("Data sent: ");
        Serial.println(Data);
        
}

and this is the error I'm getting:

Connected...
Making HTTP request...
HTTP request sent
HTTP request received
HTTP/1.1 400 Bad Request
Date: Sat, 26 May 2018 03:04:19 GMT
Server: Apache/2.4.33 (Win32) OpenSSL/1.0.2n PHP/7.1.16
Vary: accept-language,accept-charset
Accept-Ranges: bytes
Connection: close
Content-Type: text/html; charset=utf-8
Content-Language: en
Expires: Sat, 26 May 2018 03:04:19 GMT

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Bad request!</title>
<link rev="made" href="mailto:postmaster@localhost" />
<style type="text/css"><!--/*--><![CDATA[/*><!--*/ 
    body { color: #000000; background-color: #FFFFFF; }
    a:link { color: #0000CC; }
    p, address {margin-left: 3em;}
    span {font-size: smaller;}
/*]]>*/--></style>
</head>

<body>
<h1>Bad request!</h1>
<p>


    Your browser (or proxy) sent a request that
    this server could not understand.

</p>
<p>
If you think this is a server error, please contact
the <a href="mailto:postmaster@localhost">webmaster</a>.

</p>

<h2>Error 400</h2>
<address>
  <a href="/">localhost</a>

  <span>Apache/2.4.33 (Win32) OpenSSL/1.0.2n PHP/7.1.16</span>
</address>
</body>
</html>

Data sent: TEST

I'm not sure what I'm doing wrong since all the tutorials I've seen online seem to have the same or similar codes.

My firewall is off and I could successfully insert data into my database if I type "192.168.1.6/insert.php?data=whatever" on any browser.

Any help would be greatly appreciated.

My guess is that there is something about your request that is bad.

            client.print("GET /insert.php?data="); //php code for database
            client.print(Data); //data
            client.print(" HTTP/1.1");
            client.print("Host: ");
            client.println(server);
            client.println("Connection: close");
            client.println();

Perhaps it should look more like the BasicHttpClient example from the ESP8266 library:

void loop() {
    // wait for WiFi connection
    if((WiFiMulti.run() == WL_CONNECTED)) {


        HTTPClient http;


        USE_SERIAL.print("[HTTP] begin...\n");
        // configure traged server and url
        //http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
        http.begin("http://192.168.1.12/test.html"); //HTTP


        USE_SERIAL.print("[HTTP] GET...\n");
        // start connection and send HTTP header
        int httpCode = http.GET();


        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);


            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
            }
        } else {
            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }


        http.end();
    }


    delay(10000);
}

I'd guess that at least you have one problem here:

This:

client.print(" HTTP/1.1");

should be :

client.println(" HTTP/1.1");

"GET /insert.php?data="

You should use HTTP POST for inserting data, not GET.
The HTTP specification requires GET requests to be safe and idempotent. RFC 7231 - Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content

The problem is that you didn't finish the request line with a newline + carriage return before sending the host header.

Pieter

6v6gt:
I'd guess that at least you have one problem here:

This:

client.print(" HTTP/1.1");

should be :

client.println(" HTTP/1.1");

Wow, that was it! And I've been stuck for forever, thank you so so much! And thanks to everyone for helping out!