learn-php Posted February 15, 2021 Share Posted February 15, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312155-http-post-undefined-index-error/ Share on other sites More sharing options...
gw1500se Posted February 15, 2021 Share Posted February 15, 2021 I don't see how you are getting to line 14 at all. You are checking for POST but the URL is GET. Perhaps you should be using this: if (isset($api_key)) { Quote Link to comment https://forums.phpfreaks.com/topic/312155-http-post-undefined-index-error/#findComment-1584487 Share on other sites More sharing options...
learn-php Posted February 15, 2021 Author Share Posted February 15, 2021 (edited) 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 February 15, 2021 by learn-php Quote Link to comment https://forums.phpfreaks.com/topic/312155-http-post-undefined-index-error/#findComment-1584488 Share on other sites More sharing options...
gw1500se Posted February 15, 2021 Share Posted February 15, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312155-http-post-undefined-index-error/#findComment-1584489 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.