<?php
$result = [
'success' => false,
'resultCode' => 400,
'errorMsg' => '資料不足',
];
if(! isset($from_me)){
echo json_encode($result, JSON_UNESCAPED_UNICODE);
exit;
}
$entityBody = file_get_contents('php://input');
$bdata = json_decode($entityBody, true);
$result['data_from'] = $bdata;
$reuire_fields = [
'user_name',
'user_phone',
'user_email',
'user_password',
];
foreach($reuire_fields as $rf){
if(empty($bdata[$rf])){
$result['resultCode'] = 405;
$result['errorMsg'] = $rf. '為必要欄位';
echo json_encode($result, JSON_UNESCAPED_UNICODE);
exit;
}
}
$sql = "UPDATE `user_data` SET
`user_name`=?,
`user_phone`=?,
`user_email`=?
WHERE user_id=? AND `user_password`=?";
$stmt = $pdo->prepare($sql);
$stmt->execute([
$bdata['user_name'],
$bdata['user_phone'],
$bdata['user_email'],
$_SESSION['user']['user_id'],
$bdata['user_password'],
]);
if($stmt->rowCount()==1){
$result['success'] = true;
$result['resultCode'] = 200;
$result['errorMsg'] = '';
} else {
$result['resultCode'] = 406;
$result['errorMsg'] = '修改沒有成功';
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
以上是我的PHP的程式碼
我用POSTMAN FORM-DATA
這些欄位PUT給PHP (VALUE都有給值)
KEY
'user_name',
'user_phone',
'user_email',
'user_password',
條件是如果為空值才會跑到會讀出
{"success":false,"resultCode":405,"errorMsg":"user_name為必要欄位","data_from":null}
更新
跑出這一段就表示他根本沒有接受到我POSTMAN PUT給他的值
是我用錯方式嘛 QQ?
11/23
新增postman照片
https://lornajane.net/posts/2008/accessing-incoming-put-data-from-php
會不會是像文章內一樣要用這種方式取值?
parse_str(file_get_contents("php://input"),$post_vars);