Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

angularjs

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);

?>
like image 748
Raju Kushwaha Avatar asked Sep 15 '26 22:09

Raju Kushwaha


1 Answers

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

like image 134
Ashfaq Avatar answered Sep 18 '26 10:09

Ashfaq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!