json_decode(file_get_contents(“php:// input”))无效

时间:2017-10-25 16:53:18

标签: php angularjs

我们正在创建添加购物车应用程序,试图在addremove.php文件中获取非对象错误的属性并且$ http无效,$ data = json_decode(file_get_contents(“php:// input”));是空的 我无法使用angularjs& amp连接将日期插入sql server PHP。 我想知道如何在sql中插入数据并从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);

?>

0 个答案:

没有答案