Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get values through post method from URL

Tags:

json

post

php

mysql

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.

Postman Screenshot enter image description here

like image 901
Melvin Avatar asked Aug 26 '16 10:08

Melvin


People also ask

How can we access the data sent through the URL with the POST method?

The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure. The PHP provides $_POST associative array to access all the sent information using POST method.

Can we send parameters in POST request?

In a POST request, the parameters are sent as a body of the request, after the headers. To do a POST with HttpURLConnection, you need to write the parameters to the connection after you have opened the connection.

How do you send data in the body of POST request?

Short answer: in POST requests, values are sent in the "body" of the request. With web-forms they are most likely sent with a media type of application/x-www-form-urlencoded or multipart/form-data .

How do I send a POST request URL?

POST request in itself means sending information in the body. I found a fairly simple way to do this. Use Postman by Google, which allows you to specify the content-type (a header field) as application/json and then provide name-value pairs as parameters. Just use your URL in the place of theirs.


Video Answer


1 Answers

Looking at your screen shot, you have not passed body key values, instead you passed params.

enter image description here Click on Body Tab and then pass key & value pair.

like image 177
mukund Avatar answered Oct 03 '22 23:10

mukund