POST data not being received by PHP file.

Problems with the Windows version of XAMPP, questions, comments, and anything related.

POST data not being received by PHP file.

Postby David-OLW » 25. May 2022 16:28

I have a PIC micro controller 18F47K40 controlling a Solar tracker and want to send POST data from it
to my PC, I am using a ESP8266 Wireless unit with AT commands. I installed xampp and
have set up a database, originally there was 8 variables but because I could not get
it working, have just set up a Test with just 3 variables, ID + Col_1 and Col_2.
I can connect OK to the server and get it to download html pages. The problem is I
cannot get the GET or POST to work, I thought with just AT Commands it would be quite
simple to do it, following a HTTP POST example. I get a 200 OK and it does access the
PHP file and Database but the data is empty. The data is reaching the server but not
being sent to the php file, but I cannot find out why. If I remove "isset" it comes up
with undefined variables. I have tried different arg separators.

Commands being sent below:

Code: Select all
HSerOut ["AT+CIPSTART=0,"TCP","192.168.1.103",80"\r\n"]
HSerOut ["AT+CIPSEND=0,54\r\n"]
HSerOut ["POST /insert.php HTTP/1.1\r\n"]
HSerOut ["Host: 192.168.1.103\r\n"]
HSerOut ["\r\n"]
HSerOut ["\r\n"]
   Note: Value1 = 54 Value2 = 61
HSerOut ["Content-Length: 19\r\n"]
HSerOut ["\r\n"]
HSerOut ["\r\n"]
HSerOut ["Col_1=",Dec Value1,"&Col_2=",Dec Value2,"\r\n"]
HSerOut ["\r\n"]
HSerOut ["\r\n"]

Reply recived:-

AT+CIPSTART=0,"TCP","192.168.1.103",80
0,CONNECT

OK

Content-Length: 19
 
Col_1=54&Col_2=61

Recv 54 bytes

SEND OK

+IPD,0,225:HTTP/1.1 200 OK
Date: Wed, 25 May 2022 14:28:45 GMT
Server: Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/8.1.5
X-Powered-By: PHP/8.1.5
Content-Length: 28
Content-Type: text/html; charset=UTF-8

 record created successfully
0,CLOSED


PHP File "insert.php":-

<?php
$servername = "192.168.1.103:3306";
$username = "root";
$password = "xxxxxxxxxxxx";
$dbname = "test_esp";

//var_dump($_GLOBALS);
//var_dump($_POST);
  // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

/* * Get data out of $_POST array
* and do a query to insert it into test_scores table *
*/
$Col_1 = isset($_POST['Col_1']);
$Col_2 = isset($_POST['Col_2']);

    $sql = "INSERT INTO esp_table (Col_1, Col_2)
        VALUES ('" . $Col_1 . "', '" . $Col_2 . "')";
     
  if ($conn->query($sql) === TRUE) {
            echo " record created successfully";
        }
        else {
            echo "Error: " . $sql . "<br>" . $conn->error;
 
 $conn->close();

 }


The ESP8266 Docs state to include before data "Content-Length: 19\r\n"
but when I put it in I got from Access Log:-
192.168.1.121 - - [24/May/2022:15:02:15 +0100] "POST /insert.php HTTP/1.1" 200 41 "-" "ESP8266"
192.168.1.121 - - [24/May/2022:15:02:15 +0100] "Content-Length: 19" 400 329 "-" "-"
Then commenting it out I got:-
192.168.1.121 - - [24/May/2022:15:04:54 +0100] "POST /insert.php HTTP/1.1" 200 41 "-" "ESP8266"
192.168.1.121 - - [24/May/2022:15:04:54 +0100] "Col_1=54&Col_2=61" 400 329 "-" "-"
This line seems to prove data is being received by server. "Col_1=54&Col_2=61"
But then putting a second carriage return/line feed after each I now just get 200 OK as above.

On the reply from server the Time is 1 hour earlier, it is correct in the Access Log.
I do not know whether the format I am sending is wrong, I have tried to monitor what Xampp is actually
receiving but cannot find a way to do it.
Any ideas or help/suggestions Thanks.
David-OLW
 
Posts: 5
Joined: 24. May 2022 18:15
XAMPP version: 8.1.5.
Operating System: Windows 7

Re: POST data not being received by PHP file.

Postby Nobbie » 26. May 2022 10:37

The assignment via isset is wrong. There you only assign TRUE or FALSE instead of the value. It must be like this :

if (isset($_POST['Col_1']))
$Col_1 = $_POST['Col_1'];

And what did var_ dump() show?

P.S.: I think the main problem is your HTTP Header lacks a Content-Type. If you like to POST data like a HTML form, you must apply the appropriate

Content-Type: application/x-www-form-urlencoded

As you do not transfer lots of data, i think its easier to send a GET Request instead of POST. You also should use HTTP/1.0 instead of HTTP/1.1, there is no HOST header and no Content-Length (in case of POST) and HTTP/1.1 can be chunked what is a little tricky. But GET is much easier, simply pass the parameter in the URL:

GET /insert.php?Col_1=54&Col_2=61 HTTP/1.0

Thats all, you may add a FROM: and/or a User-Agent: as headers, but its not a must.
Nobbie
 
Posts: 13165
Joined: 09. March 2008 13:04

Re: POST data not being received by PHP file.

Postby David-OLW » 26. May 2022 16:32

Many thanks for reply. Have altered php file. var_dump() of the $_POST data = "array(0) { }"


When sending "GET /insert.php?Col_1=54&Col_2=61 HTTP/1.0" I get:-

Access log
192.168.1.121 - - [26/May/2022:14:55:27 +0100] "GET /insert.php?Col_1=54&Col_2=61 HTTP/1.0GET" 408 324 "-" "-"

With POST I did try "Content-Type: application/x-www-form-urlencoded" and "Content-Type: text/plain".

With no Host and no Content-Length I only get this:-

Access log
192.168.1.121 - - [26/May/2022:14:29:41 +0100] "POST /insert.php HTTP/1.0" 408 324 "-" "-"

and it does not access php file, adding Content-Length I get 200 OK but with error:-

Access Log
192.168.1.121 - - [26/May/2022:15:49:24 +0100] "POST /insert.php HTTP/1.0" 200 252 "-" "-"

Error Log
[Thu May 26 15:49:24.206600 2022] [php:warn] [pid 8132:tid 1536] [client 192.168.1.121:44367] PHP Warning: Undefined array key "Col_2" in C:\\xampp\\htdocs\\insert.php on line 21
[Thu May 26 15:49:24.207600 2022] [php:warn] [pid 8132:tid 1536] [client 192.168.1.121:44367] PHP Warning: Undefined variable $Col_1 in C:\\xampp\\htdocs\\insert.php on line 24

Thanks David
David-OLW
 
Posts: 5
Joined: 24. May 2022 18:15
XAMPP version: 8.1.5.
Operating System: Windows 7

Re: POST data not being received by PHP file.

Postby Nobbie » 26. May 2022 19:16

I cannot help with "Prosa" ("told" code).

Please show FULL code for each case. And please not modified, real code.

Of course you know that you cannot use $_POST in case of GET request?! And if in doubt, you might use $_REQUEST instead of $_GET / $_POST. For me it looks as if you are "mixing" errors with correct code, here and there and everywhere... Without code i cannot do anything.
Nobbie
 
Posts: 13165
Joined: 09. March 2008 13:04

Re: POST data not being received by PHP file.

Postby David-OLW » 27. May 2022 21:26

Thank for your help in solving this
Code attached GET first:-
PHP file
Code: Select all
<?php
$servername = "192.168.1.103:3306";
$username = "root";
$password = "xxxxxxxxxxx";
$dbname = "test_esp";

var_dump($_GET);
  // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

/* * Get data out of $_GET array
* and do a query to insert it into esp_table table *
*/
if (isset($_GET['Col_1']))
$Col_1 = ($_GET['Col_1']);
$Col_2 = ($_GET['Col_2']);

    $sql = "INSERT INTO esp_table (Col_1, Col_2)
        VALUES ('" . $Col_1 . "', '" . $Col_2 . "')";
     
  if ($conn->query($sql) === TRUE) {
            echo " record created successfully";
        }
        else {
            echo "Error: " . $sql . "<br>" . $conn->error;
 
 $conn->close();

 }


PIC Code:-
Code: Select all
HSerOut ["AT+CIPSTART=0,",34,"TCP",34,",",34,"192.168.1.103",34,",","80","\r\n"]
HSerOut ["AT+CIPSEND=0,76\r\n"]
HSerIn 10000, ESP_Tout900, [Wait (">")]
HSerOut ["GET /insert-g.php?Col_1=54&Col_2=61 HTTP/1.0\r\n"]
HSerOut ["Host: 192.168.1.103:80\r\n"]
HSerOut ["Accept: */*\r\n"]
HSerOut ["\r\n"]


Reply received:-
Code: Select all
AT+CIPSTART=0,"TCP","192.168.1.103",80
0,CONNECT

OK

Recv 76 bytes

SEND OK

Access Log
192.168.1.121 - - [27/May/2022:20:35:33 +0100] "GET /insert-g.php?Col_1=54&Col_2=61 HTTP/1.0" 408 324 "-" "-"
Error Log
[Fri May 27 20:40:34.322600 2022] [core:debug] [pid 11616:tid 1536] protocol.c(1479): [client 192.168.1.121:9860] AH00567: request failed: error reading the headers

POST without "Content-Length:-
PHP file
Code: Select all
<?php
$servername = "192.168.1.103:3306";
$username = "root";
$password = "xxxxxxxxxx";
$dbname = "test_esp";
var_dump($_POST);
  // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

/* * Get data out of $_POST array
* and do a query to insert it into esp_table table *
*/
if (isset($_POST['Col_1']))
$Col_1 = $_POST['Col_1'];
$Col_2 = $_POST['Col_2'];

    $sql = "INSERT INTO esp_table (Col_1, Col_2)
        VALUES ('" . $Col_1 . "', '" . $Col_2 . "')";
     
  if ($conn->query($sql) === TRUE) {
            echo " record created successfully";
        }
        else {
            echo "Error: " . $sql . "<br>" . $conn->error;
 
 $conn->close();

 }


PIC Code:-
Code: Select all
HSerOut ["AT+CIPSTART=0,",34,"TCP",34,",",34,"192.168.1.103",34,",","80","\r\n"]
HSerOut ["AT+CIPSEND=0,97\r\n"]
HSerIn 10000, ESP_Tout100, [Wait (">")]
HSerOut ["POST /insert.php HTTP/1.0\r\n"]
HSerOut ["Host: 192.168.1.103:80\r\n"]
HSerOut ["Content-Type: application/x-www-form-urlencoded\r\n"]
HSerOut ["\r\n"]
HSerOut ["Col_1=54&Col_2=61\r\n"]
HSerOut ["\r\n"]


reply received:-
Code: Select all
AT+CIPSTART=0,"TCP","192.168.1.103",80
0,CONNECT

OK

Recv 97 bytes

Col_1=54&Col_2=61

SEND OK


Access Log
192.168.1.121 - - [27/May/2022:19:55:16 +0100] "POST /insert.php HTTP/1.0" 408 324 "-" "-"
Error Log
[Fri May 27 20:00:16.504600 2022] [core:debug] [pid 11616:tid 1536] protocol.c(1479): [client 192.168.1.121:46848] AH00567: request failed: error reading the headers

POST with "Content-Length"
PIC Code
Code: Select all
HSerOut ["AT+CIPSTART=0,",34,"TCP",34,",",34,"192.168.1.103",34,",","80","\r\n"]
HSerOut ["AT+CIPSEND=0,118\r\n"]
HSerIn 10000, ESP_Tout100, [Wait (">")]
HSerOut ["POST /insert.php HTTP/1.0\r\n"]
HSerOut ["Host: 192.168.1.103:80\r\n"]
HSerOut ["Content-Type: application/x-www-form-urlencoded\r\n"]
HSerOut ["\r\n"]
HSerOut ["Content-Length: 19\r\n"]
HSerOut ["\r\n"]
HSerOut ["Col_1=54&Col_2=61\r\n"]
HSerOut ["\r\n"]


Reply received:-
Code: Select all
AT+CIPSTART=0,"TCP","192.168.1.103",80
0,CONNECT

OK

Col_1=54&Col_2=61


Recv 118 bytes

SEND OK

+IPD,0,482:HTTP/1.1 200 OK
Date: Fri, 27 May 2022 19:10:37 GMT
Server: Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/8.1.5
X-Powered-By: PHP/8.1.5
Content-Length: 265
Connection: close
Content-Type: text/html; charset=UTF-8

array(0) {
}
<br />
<b>Warning</b>:  Undefined array key "Col_2" in <b>C:\xampp\htdocs\insert.php</b> on line <b>20</b><br />
<br />
<b>Warning</b>:  Undefined variable $Col_1 in <b>C:\xampp\htdocs\insert.php</b> on line <b>23</b><br />
 record created successfully
0,CLOSED


Access Log
192.168.1.121 - - [27/May/2022:20:10:37 +0100] "POST /insert.php HTTP/1.0" 200 265 "-" "-"
192.168.1.121 - - [27/May/2022:20:13:38 +0100] "POST /insert.php HTTP/1.0" 408 324 "-" "-"
Error Log
[Fri May 27 20:10:37.699600 2022] [authz_core:debug] [pid 11616:tid 1536] mod_authz_core.c(815): [client 192.168.1.121:2492] AH01626: authorization result of Require all granted: granted
[Fri May 27 20:10:37.699600 2022] [authz_core:debug] [pid 11616:tid 1536] mod_authz_core.c(815): [client 192.168.1.121:2492] AH01626: authorization result of <RequireAny>: granted
[Fri May 27 20:10:37.711600 2022] [php:warn] [pid 11616:tid 1536] [client 192.168.1.121:2492] PHP Warning: Undefined array key "Col_2" in C:\\xampp\\htdocs\\insert.php on line 20
[Fri May 27 20:10:37.712600 2022] [php:warn] [pid 11616:tid 1536] [client 192.168.1.121:2492] PHP Warning: Undefined variable $Col_1 in C:\\xampp\\htdocs\\insert.php on line 23
[Fri May 27 20:18:39.383600 2022] [core:debug] [pid 11616:tid 1536] protocol.c(1479): [client 192.168.1.121:24333] AH00567: request failed: error reading the headers

Thanks David
David-OLW
 
Posts: 5
Joined: 24. May 2022 18:15
XAMPP version: 8.1.5.
Operating System: Windows 7

Re: POST data not being received by PHP file.

Postby Nobbie » 28. May 2022 11:15

You did not follow my advice, as i said, there is no Host Header for HTTP 1/.0. You MUST NOT send a Host header.

In the PHP code you missed one isset for Col _2 parameter. Dont you know PHP?
Nobbie
 
Posts: 13165
Joined: 09. March 2008 13:04

Re: POST data not being received by PHP file.

Postby Nobbie » 28. May 2022 13:58

FInally i did a small test (i do not have that micro controller, instead i wrote a PHP script as client), the Header 408 means timeout you only need to send two newlines at the end.

Code: Select all
...
HSerOut ["GET /insert-g.php?Col_1=54&Col_2=64 HTTP/1.0\r\n\r\n"]
...


No headers, no HOST, no Accept, no Content-Type, nothing. Its so simple.

P.S.: In your code, you are counting wrong.

Code: Select all
HSerOut ["AT+CIPSEND=0,76\r\n"]


The 76 is the wrong number, i count more than 80 characters. You have to count \r\n as well (2 characters). With the wrong number of characters in the CIPSEND command, the request gets corrupted (it lacks the last characters). That is also a problem of your code. I finally think, that you insert far to many empty lines like this:

Code: Select all
HSerOut ["Host: 192.168.1.103:80\r\n"]
HSerOut ["Content-Type: application/x-www-form-urlencoded\r\n"]
HSerOut ["\r\n"]
HSerOut ["Content-Length: 19\r\n"]
HSerOut ["\r\n"]
HSerOut ["Col_1=54&Col_2=61\r\n"]
HSerOut ["\r\n"]


That looks wrong. There is already \r\n at the end of each line, you MUST NOT add empty lines. It should look like this instead:

Code: Select all
HSerOut ["Host: 192.168.1.103:80\r\n"]
HSerOut ["Content-Type: application/x-www-form-urlencoded\r\n"]
HSerOut ["Content-Length: 19\r\n"]
HSerOut ["\r\n"]
HSerOut ["Col_1=54&Col_2=61\r\n"]


Only at the end of the headers you need an extra empty line (as above). You really have TONS of errors in your code. You finally may keep HTTP/1.1 and the HOST: header, but you MUST provide a correct request and you have to count correctly for the CIPSEND. I still recommend GET and HTTP/1.0, thats the easiest request (see above).
Nobbie
 
Posts: 13165
Joined: 09. March 2008 13:04

Re: POST data not being received by PHP file.

Postby David-OLW » 28. May 2022 18:35

Thanks Nobbie Great, yes that works with HTTP/1.0 and GET with parameters.

Cannot get POST to work even with HTTP/1.0, same errors.

As stated in my first post there will be 8 values to send (Solar Tracker Project)
I assume that will be ok with GET.

Many thanks David
David-OLW
 
Posts: 5
Joined: 24. May 2022 18:15
XAMPP version: 8.1.5.
Operating System: Windows 7

Re: POST data not being received by PHP file.

Postby David-OLW » 31. May 2022 14:25

Just to add I did get POST working OK.
It reqires sending all the request/headers/data including /r/n's in one String.
The seperate "HSerout" adds spaces between which xampp cannot understand and gives
"field name is malformed" or "error reading Header".
Thanks David
David-OLW
 
Posts: 5
Joined: 24. May 2022 18:15
XAMPP version: 8.1.5.
Operating System: Windows 7

Re: POST data not being received by PHP file.

Postby Nobbie » 31. May 2022 15:02

David-OLW wrote:The seperate "HSerout" adds spaces between


Ouch - that sounds like an ugly bug. Hard to believe, but if you can reproduce it, so why not?! Actually you then can use either one, POST or GET, it does not matter. The GET request is limited to a certain amount of bytes, round about 1kb or so, whereas POST can send huge files if needed. But for limited amount of data (in your case 8 variables and probably not carrying lots of data) simply use GET. In a real life scenario the GET request is limited by the browser, how many bytes you can put into the URL of the address bar of the browser?! The POST data is not transferred via address bar, but as a stream directly from the client (browser) to the server.
Nobbie
 
Posts: 13165
Joined: 09. March 2008 13:04

Re: POST data not being received by PHP file.

Postby dbw123 » 23. April 2023 04:23

Hai David-OLW
can you share the get & post data AT Commands with php
i am unable to POST & GET data using ESP8266 with PIC
can you help pls
dbw123
 
Posts: 1
Joined: 23. April 2023 03:31
XAMPP version: 1.8.2
Operating System: windows 7 32 bit


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 106 guests