Issue
SQL Tablemy loadtable function
If i delete the button in the table . through the id the sql database didnt get delete
I have searched a lot about this related article . . I couldn’t find the solution
passing my id in http delete request. when I have var_dump the variable its giving empty string.
It didn’t give error it is just gave data deleted correctly message. but didn’t get deleted
<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';
}
}
Debugging info:
Warning provided by PHP:
Warning: Attempt to read property "send_id" on string in
C:\xampp\htdocs\delete.php on line 12
Console log results:
console.log(id) // 123
console.log(typeof(id)// string
Solution
You’re literally sending "123" by itself to the server. That isn’t JSON, and it isn’t an object, it’s just a plain string.
So I suggest
-
remove
headers: { 'Content-type': 'application/json;charset=utf-8' }
from the $http options, -
change
$data = json_decode(file_get_contents("php://input"));
to just$id_del = file_get_contents("php://input");
-
remove
$id_del = $data->send_id;
-
change
if(is_null($data)){
toif(is_null($id_del)){
and changevar_dump($data);//empty string if(count(array($data)) > 0) {
tovar_dump($id_del);//empty string if(!empty($id_del) {
This will make PHP read just the plain string and use that as the ID, without attempting to decode it as JSON or extract values from within it.
Answered By – ADyson
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0