Get values through post method from URL
Solution 1
As per your screenshot you are sending your empid
through query parameter so you need to access that as follows
<?php
if (isset($_GET['empid'])) {
echo $_GET['empid'];
}else{
// else part
}
also for that you need to Request Url in Postman using GET
method.
But as you have stated that you want to send empid
through POST
in postman, you have to send it through form-data
in Postman and access it as $_POST["empid"];
. following is the screenshot for your reference
else there is another option where you can send the POST
data through body
as row json
and access it as
$rawPostData = file_get_contents('php://input');
$jsonData = json_decode($rawPostData);
and $post
will contain the raw data
. And you can send it through postman as in following screenshot.
Solution 2
Looking at your screen shot, you have not passed body key values, instead you passed params.
Click on Body Tab and then pass key & value pair.
Solution 3
You have to set the Body to "x-www-form-urlencoded" and adding the variables to be posted
Or try this SO question, its already been answered
Solution 4
I replicated the code and db on my system to figure out the problem. I also added some lines of code before if (isset($_POST['empid'])) {
for diagnostics sake:
$method = $_SERVER['REQUEST_METHOD'];
echo $method."<br/>";
The application file is index.php
deployed in json
directory inside webroot.
When I send any request to http://localhost/json directory (either POST/GET), Apache redirects the request as a GET request to index.php (as configured in my Apache config file). I assume this is what you're experiencing.
But when I send the request to http://localhost/json/index.php, the request is accurately received and processed.
Therefore, I would say the solution is that you need to specify the php
file and also set the empid
parameter as part of the body in Postman (not as part of the url).
Melvin
Updated on January 19, 2020Comments
-
Melvin over 4 years
I am trying some code to get value from URL through post method and search database table for that value and get info from the database and encode it into JSON response.
Here is my code :
<?php //open connection to mysql db $connection = mysqli_connect("localhost","root","","json") or die("Error " . mysqli_error($connection)); if (isset($_POST['empid'])) { $k = $_POST['empid']; //fetch table rows from mysql db $sql = "select `salary` from tbl_employee where `employee_id` = $k "; } else { //fetch table rows from mysql db $sql = "select `salary` from tbl_employee"; } //fetch table rows from mysql db $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection)); //create an array $emparray = array(); while($row =mysqli_fetch_assoc($result)) { $emparray[] = $row; } echo json_encode($emparray); //close the db connection mysqli_close($connection); ?>
I used Postman extension on Chrome and pass the values but it is not returning. Instead it is returning the else part.
-
mukund over 7 yearsTry to check with <?php $postdata = file_get_contents("php://input"); ?> Are you actually receiving any data or not
-
Melvin over 7 yearsi am getting the else condition
-
mukund over 7 yearsI mean what you received in postdata as per above, because setting shown in screenshot are enough to receive post data - Body - key value - select form data
-
Melvin over 7 yearsI am getting all data as result when i tried your solution
-
mukund over 7 yearsYou mean you received expected data in that as string?
-
Melvin over 7 yearsno i did not get . I got all the data instead which i used in else part
-
Melvin over 7 yearsAre you getting the result ?
-
hasan movahed over 7 yearsbut you do not forget SQL injection
-
Nikhil over 7 years@Melvin did my answer help you?