简体   繁体   中英

json_decode(file_get_contents("php://input")) not working

we are creating add cart application, Trying to get property of non-object error in addremove.php file and $http is not working, $data=json_decode(file_get_contents("php://input")); are null I am unable to connect insert the date into sql server using angularjs & php. I want to know how to insert data in sql and fetch the data from db. Index.js

//Include the module as a dependency of our main AngularJS app module
var app = angular.module('notesApp', [
    'ngRoute'
]);

// App routing
app.config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: "home.html"
        })

        .when('/main/:itemn', {
            templateUrl: "index1.html"
        })
        .when('/cart', {
            templateUrl: "cart.html"
        })
            .when('/test', {
            templateUrl: "addremove.php"
        })
            .when('/addremove', {
            templateUrl: "addremove.php"
        })


}]);
       //main controller
app.controller("mainController", ['$scope', '$http', '$routeParams', '$location', '$window', function($scope, $http, $routeParams, $location, $window) {
$scope.products = [];
var arr = $location.$$path.split('/');
    $scope.itemn = arr[2];
    // move to new url
    $scope.go = function(path) {
        $location.path(path);
    };
      $http.get('modeldata.php').then(function(response) {
        $scope.items = response.data.records;

    }, function(errResponse) {
        console.error('Error while fetching notes');
    });
 $http.get('cart.php').then(function(response) {
        $scope.carts = response.data.records;
          // $scope.products=$scope.items;
    }, function(errResponse) {
        console.error('Error while fetching notes');
    });
        // Add new record
 $scope.addItem = function(item){
    var variableName = {'a':'1'};
 $http({
   method: 'post',
   url: 'addremove.php',
   data: variableName,
   headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  })
  $scope.go('/test');

 }

}]);


data are flow in addremove file to insert into mysql server table

// addremove.php file

<?php
$servername = "XXXXX";
$username = "XXXXX";
$password = "XXXXX";
$dbname = "XXXXXXX";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$data = json_decode(file_get_contents("php://input"));
echo $data;
$itemno = $data->a;
$query="INSERT INTO cart(itemno) values ( '$itemno')";
 if (mysqli_query($conn, $query)) {
        echo 'Data Deleted Successfully...';
    } else {
        echo 'Failed';
    }

 $lastinsert_id = mysqli_insert_id($conn);

?>

It could be server cache issue. Try to clear php cache. I hope It will fix.

It could be php and server cache issue. You can clear cache and Here is another solution, we fixed same type of error in this way.

$data = json_decode(file_get_contents("php://filter"));

You need to execute this line then revert back and then try :

//It is just to check file_get_contents function is working.
$data = file_get_contents('provide_any_file.txt')

$data = json_decode(file_get_contents("php://fd"));

Now, again revert back to :

$data = json_decode(file_get_contents("php://input"));

It will clear cache in strange way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM