我如何正确使用 AngularJS $http.post 通过 PHP 更新 MySQLi table?
How do I properly use AngularJS $http.post to update MySQLi table through PHP?
我想通过 PHP 更新一个 MySQLi table achievements
。该应用程序在 AngularJS 中编码并测量各种统计数据。当达到目标时,我使用 AngularJS 的 $http.post
将信息发送到 PHP 脚本。 PHP 然后应该处理信息并相应地更新我的 table。 $http.post
returns 成功消息,但 table 未更新。我确信数据库连接信息是正确的。
我的 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 Notice: Trying to get property 'table' of non-object in /update-data.php on line 16
。
信息发布的方式或检索发布的信息的方式肯定有问题,而不是 SQL。
提前感谢您在解决这种情况时可能提供的任何帮助!
可能错误在 $.param() 中。
data: $.param({ table: table.toString(), column: column.toString(), value: value.toString(), whereColumn: whereColumn.toString(), whereValue: whereValue.toString() })
$.param() 是一个 JQuery 函数。
检查这个:
AngularJSParamSerializer
尝试不使用 json encode/decode 看看是否可行。
编辑(回答者评论): "In your php try to echo file_get_contents('php://input') ;
And in javascript in the callback of the request console.log(response);
To see if some data is successfully sent."
尽量不要对数据使用 $.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.");
});
}
问题出在数据格式化的方式上。这是我打印的 $info
变量:
{"table":"achievements","column":"Goal","value":1,"whereColumn":"id","whereValue":"1"}
来自 file_get_contents('php://input')
的字符串用大括号括起来,每个值都用逗号分隔,这两个我都必须删除。
我还必须去掉每个字符串键值对的引号,因为我想要的只是值,所以我必须去掉 :
和之前的所有内容('key' 如果你愿意的话)。
这是我的(虽然有点老套)结果,效果非常好:
//PHP posted info
$info = file_get_contents('php://input');
$dataArr = explode(',',$data);
$dCount = 0;
foreach ($dataArr as $datum) {
$newDatum = substr($datum, strpos($datum, ":") + 1);
$newDatum = str_replace(array('}', '{', '"'), '', $newDatum);
$dataArr[$dCount] = $newDatum;
$dCount++;
}
$table = $dataArr[0];
$column = $dataArr[1];
$value = $dataArr[2];
$whereColumn = $dataArr[3];
$whereValue = $dataArr[4];
我想通过 PHP 更新一个 MySQLi table achievements
。该应用程序在 AngularJS 中编码并测量各种统计数据。当达到目标时,我使用 AngularJS 的 $http.post
将信息发送到 PHP 脚本。 PHP 然后应该处理信息并相应地更新我的 table。 $http.post
returns 成功消息,但 table 未更新。我确信数据库连接信息是正确的。
我的 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 Notice: Trying to get property 'table' of non-object in /update-data.php on line 16
。
信息发布的方式或检索发布的信息的方式肯定有问题,而不是 SQL。
提前感谢您在解决这种情况时可能提供的任何帮助!
可能错误在 $.param() 中。
data: $.param({ table: table.toString(), column: column.toString(), value: value.toString(), whereColumn: whereColumn.toString(), whereValue: whereValue.toString() })
$.param() 是一个 JQuery 函数。
检查这个: AngularJSParamSerializer
尝试不使用 json encode/decode 看看是否可行。
编辑(回答者评论): "In your php try to echo file_get_contents('php://input') ;
And in javascript in the callback of the request console.log(response);
To see if some data is successfully sent."
尽量不要对数据使用 $.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.");
});
}
问题出在数据格式化的方式上。这是我打印的 $info
变量:
{"table":"achievements","column":"Goal","value":1,"whereColumn":"id","whereValue":"1"}
来自 file_get_contents('php://input')
的字符串用大括号括起来,每个值都用逗号分隔,这两个我都必须删除。
我还必须去掉每个字符串键值对的引号,因为我想要的只是值,所以我必须去掉 :
和之前的所有内容('key' 如果你愿意的话)。
这是我的(虽然有点老套)结果,效果非常好:
//PHP posted info
$info = file_get_contents('php://input');
$dataArr = explode(',',$data);
$dCount = 0;
foreach ($dataArr as $datum) {
$newDatum = substr($datum, strpos($datum, ":") + 1);
$newDatum = str_replace(array('}', '{', '"'), '', $newDatum);
$dataArr[$dCount] = $newDatum;
$dCount++;
}
$table = $dataArr[0];
$column = $dataArr[1];
$value = $dataArr[2];
$whereColumn = $dataArr[3];
$whereValue = $dataArr[4];