Jump to content

Recommended Posts

Hello there,

 

I am coding Arduino micro controller, and intended to GET the data from server MySQL db through php to turn ON/OFF LED.

I could able to GET the data from manually updated MySQL db value, even I could able to successfully change the data of MySQL db from micro controller POST method.

But I am getting error when trying to change the MySQL db value from browser using online POST tool,

Following is PHP code at server

8  $api_key_value ="XXXXXXXXX";//Sample
9  $api_key ="";
10 $status = "";
11 $id ="";
12 
13 if ($_SERVER["REQUEST_METHOD"] == "POST") {
14    $api_key = test_input($_POST["api_key"]);
15    if($api_key == $api_key_value) {
        $id     = test_input($_POST["id"]);
        $status = test_input($_POST["status"]);

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

        //$sql = "INSERT INTO led (status)
        //VALUES ('" . $status . "')";
        $sql = "UPDATE led SET status='$status' WHERE id='$id'";

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } 
        else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    
        $conn->close();
    }
    else {
        echo "Wrong API Key provided.";
    }

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

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

Post Command:

http://url/MySQL_POST.php?api_key=XXXXXXXXX&id=1&status=on

Response/Error:

Notice: Undefined index:  api_key in <PATH>/MySQL_POST.php on line 14
Wrong API Key provided.

Could be a basic issue, but I am very new to php coding and cannot solve this problem yet.

Could someone please help?

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/312155-http-post-undefined-index-error/
Share on other sites

Hi, Thanks for your response.

I tried changing code as you mentioned also tried to echo input arguments.

It still enters condition, but did not update MySQL db as values were null.

Modified code,

8  $api_key_value ="XXXXXXXXX";
9  $api_key ="";
10 $status = "";
11 $id ="";
12 
13 if ($_SERVER["REQUEST_METHOD"] == "POST") {
14    echo "value of api_key=" . $api_key . "\n";
15    echo "value of id=" . $id . "\n";
16    echo "value of status=" . $status . "\n";
17    if (isset($api_key) and isset($id) and isset($status)) {
18        $api_key = test_input($_POST["api_key"]);
19        $id     = test_input($_POST["id"]);
20        $status = test_input($_POST["status"]);

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

        //$sql = "INSERT INTO led (status)
        //VALUES ('" . $status . "')";
        $sql = "UPDATE led SET status='$status' WHERE id='$id'";

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } 
        else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    
        $conn->close();
    }
//    else {
//        echo "Wrong API Key provided.";
//    }

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

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

 

Response:

value of api_key= value of id= value of status= Notice: Undefined index: api_key in <url>/post_test.php on line 18 Notice: Undefined index: id in <url>/post_test.php on line 19 Notice:Undefined index: status in <url>/post_test.php on line 20 New record created successfully

 

Kindly suggest any other preferred method to achieve if this is incorrect. 

Edited by learn-php

Monday morning. 🙁 I told you completely wrong since $_POST will always have a value, so you need to use 'empty' instead. Also I meant for it to replace your first 'if'.

 if (!empty($_POST["api_key")) {

That being said you still have the same problem. Since the URL is using GET you need to use the $_GET array, not $_POST.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.