file_get_contents in php returns 空字符串

file_get_contents in php returns empty string

SQL Tablemy loadtable function 如果我删除 table 中的按钮。通过 id sql 数据库没有得到 delete

关于这篇相关文章,我搜索了很多。 .我找不到解决方案 在 http 删除请求中传递我的 id。当我有 var_dump 变量时,它给出空字符串。 它没有给出错误,它只是给出了正确删除数据的消息。但没有被删除

<tr ng-repeat="x in dated">
<td>{{x.Id}}</td>
<td>{{x.Name}}</td>
<td>{{x.Phone}}</td>
<td>{{x.Status}}</td>
<td><button name="btn" value="Update" ng-click="updatefn(x.Id,x.Name,x.Phone,x.Status)">Update</button></td>
<td><button name="btn" value="Delete" ng-click="deletefn(x.Id)">Delete</button></td>

</tr>


$scope.loadtable=function()
{
 $http({
  url:'select.php' ,
  method: 'GET', 
}).then(function(get_response){
    alert(JSON.stringify(get_response));
    $scope.dated=get_response.data;
    $scope.status=get_response.status;
    $scope.value="ADD";
                           },function(get_response)
{
});
}


$scope.deletefn=function(id)
{
$http({
url: 'delete.php',
method:'DELETE',
data:id,
headers: {
        'Content-type': 'application/json;charset=utf-8' }
  
}).then(function(res)
{
    console.log(JSON.stringify(res)
    
    );
},function(res)
{
    alert(res);
});

$data = json_decode(file_get_contents("php://input")); 
if(is_null($data)){
    echo 'This line is printed, because the $var is null.';
}
var_dump($data);//empty string
 if(count(array($data)) > 0)  
 {  
 $id_del = $data->send_id;
      
 $query = "DELETE FROM society_tour WHERE id='$id_del'";  
    
if(mysqli_query($connect, $query))  
 {  
echo 'Data Deleted correctly';  
}  
else
{  
  echo 'Error';
  }  
} 

调试信息:

PHP 提供的警告:

Warning: Attempt to read property "send_id" on string in C:\xampp\htdocs\delete.php on line 12

控制台日志结果:

console.log(id) // 123
console.log(typeof(id)// string

您实际上是在将“123”本身发送到服务器。那不是 JSON,也不是对象,它只是一个普通字符串。

所以我建议

  1. 从 $http 选项中删除 headers: { 'Content-type': 'application/json;charset=utf-8' }

  2. $data = json_decode(file_get_contents("php://input")); 更改为 $id_del = file_get_contents("php://input");

  3. 移除$id_del = $data->send_id;

  4. if(is_null($data)){改为if(is_null($id_del)){,将var_dump($data);//empty string if(count(array($data)) > 0) {改为var_dump($id_del);//empty string if(!empty($id_del) {

这将使 PHP 只读取纯字符串并将其用作 ID,而不尝试将其解码为 JSON 或从中提取值。