![](/img/trans.png)
[英]PHP : file_get_contents with json_decode not working together
[英]json_decode(file_get_contents("php://input")) not working
我們正在創建添加購物車應用程序,試圖在 addremove.php 文件中獲取非對象錯誤的屬性,並且 $http 不起作用,$data=json_decode(file_get_contents("php://input")); 為空我無法使用 angularjs 和 php 將日期插入到 sql server 中。 我想知道如何在 sql 中插入數據並從 db 中獲取數據。 索引.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 文件
<?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);
?>
可能是服務器緩存問題。 嘗試清除 php 緩存。 我希望它會修復。
可能是 php 和服務器緩存問題。 您可以清除緩存,這是另一種解決方案,我們以這種方式修復了相同類型的錯誤。
$data = json_decode(file_get_contents("php://filter"));
您需要執行此行,然后恢復,然后嘗試:
//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"));
現在,再次回到:
$data = json_decode(file_get_contents("php://input"));
它會以奇怪的方式清除緩存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.