Http request send data format

hi , I try to send sensors data to
db with http request node , connection with db is ok ,New record created successfully. but data is empty at db , i think my problem is how i send data :
{"sensor":"ran","location":"iran","value1":"60","value2":"60"}


Without sharing what the db is or how your request node is configured or what table schema your data uses, I'm not sure what you expect of people on the forum?

and this is my php :slight_smile:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   
   
        $sensor = test_input($_POST["sensor"]);
        $location = test_input($_POST["location"]);
        $value1 = test_input($_POST["value1"]);
        $value2 = test_input($_POST["value2"]);
       
        
        // Create connection
       
        require_once ('connect_db.php'); 
        
        $sql = "INSERT INTO sensor_data(sensor, location, value1, value2)
        VALUES ('" . $sensor . "', '" . $location . "', '" . $value1 . "', '" . $value2 . "')";
        
        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } 
        else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    
        $conn->close();
 

}
else {
    echo "No data posted with HTTP POST.";
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

Perhaps you should return the actual sql statement so that you can see what it did.

That didn't answer any of the questions I said needed answering.

thank you , I did it but again db values return null


i think the problem is how and which format I send data ? isn't it?
I will appreciate you if you help to solve that

I think the obvious (Node-RED centric) question is, why not simply use the node-red-node-mysql node (instead of having a PHP endpoint/app) and do it completely in Node-RED?

2 Likes

While it was good that you shared a screen shot of your http request node, no-one can read the complete value of the 'URL' option url:

Please export that node and paste the results to a reply.

this is the complete of url

http://www.tekbin.lifeandbusiness.ir/sahar/php/post-esp-data.php

thank you , but mysql is located at web server and needs remote access connection , and it is impossible to have it. so I guess i have to use http !

Short of doing all this in Node RED as @Steve-Mcl said.

Maybe echo out the $_POST - to ensure you are actually receiving it.
if you are, then the problem maybe test_input or something else on the PHP side?


<?php
 print_r($_POST);
?>

EDIT

but mysql is located at web server

i see :sweat_smile:

[
    {
        "id": "051e1f903d57ec87",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": ""
    },
    {
        "id": "f4909f0e23246419",
        "type": "change",
        "z": "051e1f903d57ec87",
        "name": "prepared feild",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\t   \"sensor\":\"ran\",\t   \"location\":\"iran\",\t   \"value1\":\"60\",\t   \"value2\":\"60\"\t}",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "header.Content-Type",
                "pt": "msg",
                "to": "application/x-www-form-urlencoded",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 460,
        "y": 200,
        "wires": [
            [
                "787bbfff8dff5524",
                "5b7630f82a96e09a"
            ]
        ]
    },
    {
        "id": "5b7630f82a96e09a",
        "type": "http request",
        "z": "051e1f903d57ec87",
        "name": "",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "http://www.tekbin.lifeandbusiness.ir/sahar/php/post-esp-data.php",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "x": 680,
        "y": 220,
        "wires": [
            [
                "af9a131e0dd59404"
            ]
        ]
    },
    {
        "id": "787bbfff8dff5524",
        "type": "debug",
        "z": "051e1f903d57ec87",
        "name": "SD2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 140,
        "wires": []
    },
    {
        "id": "af9a131e0dd59404",
        "type": "debug",
        "z": "051e1f903d57ec87",
        "name": "SD3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 800,
        "y": 340,
        "wires": []
    },
    {
        "id": "45833ea2d49495ed",
        "type": "inject",
        "z": "051e1f903d57ec87",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 110,
        "y": 260,
        "wires": [
            [
                "f4909f0e23246419"
            ]
        ]
    }
]

Forgetting the "Web Server" that provides an endpoint for your database for one second - are the Node-RED server and the mySQL database both on the same internal infrastructure/network?

no they aren't.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

1 Like

I do hope your data isn't private in any way. I also hope that, if you use a userid/password on that site that you don't use it anywhere else. If you don't use a uid/password, I trust that you don't care that anyone on the Internet could post to the db?

This is because you are connecting over the Internet to a web service without using TLS encryption (HTTPS). That means that you should assume that any id/password has been compromised due to interception.

1 Like

With all the input, this might have been missed

1 Like

this is just for test and it doesnt have important data , thank you for you attention

Another observation....

header.Content-Type

Should be
headers.Content-Type

header should be be plural

Its very possible, the http request node, is submitting a JSON formatted body, and not x-www-form-urlencode

Therefore correct the header key or use PHP's json_decode method

example: json_decode(file_get_contents('php://input'));

2 Likes

it really work . thank you