dthhlf1777 2016-09-14 06:35
浏览 37
已采纳

使用API​​网址时,PHP _POST ['field']为空

I followed the below online tutorial to build a PHP API.

https://trinitytuts.com/restful-web-services-with-php-mysqli/

The HTTP request works perfectly when I use Chrome's "Advanced Rest Client". I get the following response:

{
"status": 1
"msg": "Your record inserted successfully"
}

enter image description here

I tried to put the following URL directly into my web browser:

http://localhost/API/insert.php/name=paul&email=paul@domain.com&status=active

I expected the same result as I received when I used the Advanced Rest Client however, I received the following error:

Notice: Undefined index: name

I am setting the variable 'name' using the following PHP code:

$name = $_POST['name'];

My understanding is that when I paste the URL into the browser, the $name variable is not set to $_POST['name'] because entering the URL is not actually a post action.

Is it possible for me add a parameter to the URL so as to tell the browser to post? For example:

http://localhost/API/insert.php/type=POST&name=paul&email=paul@domain.com&status=active

In a nutshell, I want to be able to past a URL directly into a web browser and and not have to post it via a HTML form.

This is my PHP code for insert.php

<?php
include 'conn.php';

$name = $_POST['name'];
$email = $_POST['email'];
$status = $_POST['status'];
$id = 1;

$sql = "INSERT INTO `service`.`user` (`id`, `name`, `email`, `status`) VALUES ($id, '$name', '$email', '$status');";

if ($connection->query($sql)) {
$msg = array("status" =>1 , "msg" => "Your record inserted successfully");
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}

$json = $msg;

header('content-type: application/json');
echo json_encode($json);


@mysqli_close($conn);

?>
  • 写回答

1条回答 默认 最新

  • dpj96988 2016-09-14 06:41
    关注

    Issue has been resolved by changing $_POST to $_GET in insert.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改