【发布时间】:2020-08-22 23:04:26
【问题描述】:
处理 JSON 输入的 API 似乎存在问题。 这是我的 API(删除功能)
function doDeleteCustomer() {
global $db;
if(isParamSet(array('id'))) {
if(isParamAvailable(array('id'))) {
$customerId = $_REQUEST['id'];
$sql = "DELETE FROM customers WHERE ID=:customerid";
$stmt = $db->prepare($sql);
$stmt->execute(
array(
':customerid' => $customerId
)
);
if($stmt->rowCount() > 0) {
$response = array();
$response["error"] = false;
$response["status"] = 200;
$response["data"] = array();
$response["message"] = "Successfully removed customer!";
} else {
$response = array();
$response["error"] = true;
$response["status"] = 400;
$response["data"] = array();
$response["message"] = "Unable to remove customer!";
}
return json_encode($response);
}
}
}
当我在 Postman 中测试时,一切正常,但我已经在 x-www-form-urlencoded 中提供了信息。
但是当我想通过原始 JSON 数据提供输入时,我收到一条消息,指出缺少所需的 id 字段...我做错了什么?
【问题讨论】:
-
那么是哪个代码在做那个检查。不是您向我们展示的代码
-
@RiggsFolly。我希望我不会说任何愚蠢的话,因为我完全是 API 新手。但是你的意思是client.php吗?