我正在尝试使用Rest Api在MySQL DB中创建新记录。但是,存储在$ data中的数据不包含我通过参数输入的任何值。
我试图回声/打印以查看它包含的内容,但它什么都没有。
网址:http://localhost/api/v1/test.php?firstname=Yacub?lastname=Ali。我可以获取数据而不是POST。
function insertQueue()
{
global $connection;
$data = json_decode(file_get_contents('php://input'), true);
echo $data;
var_dump($data);
$firstname = $data["userName"];
$lastname = $data["lastname"];
$organization = $data["organization"];
$type = $data["type"];
$service = $data["service"];
echo $query="INSERT INTO queue SET
firstname='".$firstname."',
lastname='".$lastname."',
organization='".$organization."',
type='".$type."',
service='".$service."'";
if(mysqli_query($connection, $query))
{
$response=array(
'status' => 1,
'status_message' =>'Added to queue successfully.'
);
}
else
{
$response=array(
'status' => 0,
'status_message' =>'Queue Insertion has failed.'
);
}
header('Content-Type: application/json');
echo json_encode($response);
}
以下不是有效的URL。如果你想发送两者,你需要使用&
:
http://localhost/api/v1/test.php?firstname=Yacub?lastname=Ali
上面的正确的是:
http://localhost/api/v1/test.php?firstname=Yacub&lastname=Ali
其次,这是一个GET
请求。 POST
请求不会包含任何查询字符串。所以将你的<form>
的方法改为POST
:
<form action="my.php" method="post">
<!--------------------^^^^^^^^^^^^^
而不是使用这个:
$data = json_decode(file_get_contents('php://input'), true);
你可以使用:
$data = $_POST; // After you convert the form method to POST.