我将创建一个控制器,该控制器将在数据库的选定表中列出不同的选定字段,并将其传递给我的API。将参数传递给角
目前,我正在使用一个脏方法,它创建了几个控制器,其中有字段名和表名。
controller.js
.controller('ListDistinctCustomerCtrl', function($scope, $http) {
var xhr = $http({
method: 'post',
url: 'http://localhost/api/list-distinct.php?table=customer&field=cust_code'
});
xhr.success(function(data){
$scope.data = data.data;
});
})
.controller('ListDistinctSupplierCtrl', function($scope, $http) {
var xhr = $http({
method: 'post',
url: 'http://localhost/api/list-distinct.php?table=supplier&field=supp_code'
});
xhr.success(function(data){
$scope.data = data.data;
});
})
,这是API文件
列表distinct.php
<?php
require_once '/config/dbconfig.php';
$table = $_GET['table'];
$field = $_GET['field'];
GetData($table,$field);
function GetData($tablename,$fieldname) {
$sql = "SELECT distinct $fieldname as expr1 FROM $tablename order by expr1 asc";
try {
$db = getdb();
$stmt = $db->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode(array('data' => $data));
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
?>
我相信这是一个干净,更好的方式来做这个。
也许创建一个API'服务',所以你没有重复的代码 – Ties
你的问题是什么?清洁JS或PHP? – Ties
您应该*不*将您的GET参数直接解析到您的SQL查询中。 Expecialy不适用于'field' /'table'。那种安全滞后很大。 – lin