我想通过 PHP 更新 MySQLi 表 achievements
。该应用程序使用 AngularJS 编码并测量各种统计数据。当达到目标时,我使用 AngularJS 的 $http.post
将信息发送到 PHP 脚本。然后 PHP 应该处理这些信息并相应地更新我的表。 $http.post
返回成功消息,但表未更新。我确信数据库连接信息是正确的。
我的 AngularJS $scope.updatePhp
函数使用 $http.post
:
$scope.updatePhp = function(table, column, value, whereColumn, whereValue) {
$http.post(
"update-data.php", {
'table': table,
'column': column,
'value': value,
'whereColumn': whereColumn,
'whereValue': whereValue
}
);
}
我的 AngularJS $scope.updatePhp
函数没有使用 .post
快捷方式:
$scope.updatePhp = function(table, column, value, whereColumn, whereValue) {
console.log("Updating. Table: " + table + ", column: " + column + ", value: " + value + ", where: " + whereColumn + ", whereValue: " + whereValue);
$http({
method: 'POST',
url: 'update-data.php',
data: { table: table.toString(), column: column.toString(), value: value.toString(), whereColumn: whereColumn.toString(), whereValue: whereValue.toString() },
headers: { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' }
}).then(function successCallback(response) {
console.log("Success.");
}, function errorCallback(response) {
console.log("Error.");
});
}
我的整个 PHP 文件“update-data.php”:
<?php
//CORS header stuff
header("Access-Control-Allow-Origin: *");
//PHP posted info
$info = file_get_contents('php://input');
//Update the table
$hostname_DB = "databaseHost";
$database_DB = "databaseName";
$username_DB = "databaseUser";
$password_DB = "databasePass";
$portnum_DB = "databasePort";
$mysqli = mysqli_connect($hostname_DB, $username_DB, $password_DB, $database_DB, $portnum_DB ) or die(mysqli_error());
$table = $info->table;
$column = $info->column;
$value = $info->value;
$whereColumn = $info->whereColumn;
$whereValue = $info->whereValue;
$query = "UPDATE '$table' SET '$column' = '$value' WHERE '$whereColumn' = '$whereValue' ";
$mysqli->query($query) or die(mysqli_error());
?>
我正在使用 PHP error_log,每个变量 table
、column
、value
等都收到此错误:
PHP 注意:试图在第 16 行的/update-data.php 中获取非对象的属性“table”
。
信息发布的方式或检索发布的信息的方式肯定有问题,而不是 SQL。
预先感谢您在解决此问题时可能提供的任何帮助!
最佳答案
尽量不要对数据使用 $.param() 。你可以直接发送数据对象。
$scope.updatePhp = function(table, column, value, whereColumn, whereValue) {
console.log("Updating. Table: " + table + ", column: " + column + ", value: " + value + ", where: " + whereColumn + ", whereValue: " + whereValue);
$http({
method: 'POST',
url: 'update-data.php',
data: {
table: table.toString(),
column: column.toString(),
value: value.toString(),
hereColumn: whereColumn.toString(),
whereValue: whereValue.toString()
},
headers: { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' }
}).then(function successCallback(response) {
console.log("Success.");
}, function errorCallback(response) {
console.log("Error.");
});
}
关于php - 如何正确使用 AngularJS $http.post 通过 PHP 更新 MySQLi 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54210545/