我正在使用AngularJS并尝试将params发布到php程序,因此php程序可以使用一个或多个变量进行选择。
我需要传递一个json数组,它是我的sql绑定变量,它是下面的where_clause。
这是Main.php
<!DOCTYPE html>
<html >
<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http)
{
var req = {
method: 'POST',url: 'angular_master.php',
headers: {
'Content-Type': undefined
},
params: { what_to_do: "angular_users6", where_clause: '[{"sqlvalue1":"searchvalue1","sqlvalue2":"searchvalue2"}]'}
}
$http(req).then(function (response) {$scope.names = response.data.records;});
});
</script>
</body>
</html>
这是angular_master.php
的一部分if ($what_to_do == "angular_users6")
{
$json = $where_clause;
$where_array = json_decode($json,true);
error_log("\n where_clause_json : " . print_R($json,TRUE) ,3,$filename_master);
error_log("\n where_clause_array: " . print_R($where_array,TRUE) ,3,$filename_master);
$sqlvalue1 = "";
$sqlvalue2 = "";
foreach($where_array as $rows)
{
$sqlvalue1 = mysqli_real_escape_string($conn, $rows['sqlvalue1']) ;
$sqlvalue2 = mysqli_real_escape_string($conn, $rows['sqlvalue2']);
}
error_log("\n where_array: " . $sqlvalue1 . " " . $sqlvalue2 ,3,$filename_master);
我在陷阱的日志中得到以下内容 LOG:
where_clause_json : [{"sqlvalue1":"searchvalue1","sqlvalue2":"searchvalue2"}]
where_clause_array: Array
(
[0] => Array
(
[sqlvalue1] => searchvalue1
[sqlvalue2] => searchvalue2
)
)
where_array:
问题是最后一个echo语句“where_array:”(此行上方的行不显示sqlvalue1和sqlvalue2的值,尽管print_R回波显示值。
由于传递的where_clause可能包含多个值,为什么最后一个echo where_array为空。
答案 0 :(得分:0)
where_arrays = [[&#34; sqlvalue1&#34; =&GT; webcastpoa@thepentecostals.org,&#34; sqlvalue2&#34; =&GT; NA]]。
尝试foreach($ where_array [0]为$ rows)