【发布时间】:2015-07-15 21:31:07
【问题描述】:
我无法使用 Slim PHP Rest Service 和 Angular JS 添加新项目,如何将其添加到数据库中?
以下是我的代码: Slim REST 服务代码 //添加单个位置的函数
function AddLocation()
{
$app = \Slim\Slim::getInstance();
//
$request = Slim::getInstance()->request();
$location = json_decode($request->getBody());
$sql = "INSERT INTO locations (location_title, location_latitude, location_longitude') VALUES (:location_title, :location_latitude, :location_longitude)";
//
try {
$db = conn_db();
$stmt = $db->prepare($sql);
$stmt->bindParam("location_title", $location->location_title);
$stmt->bindParam("location_latitude", $location->location_latitude);
$stmt->bindParam("location_longitude", $location->location_longitude);
$stmt->execute();
$location->location_id = $db->lastInsertId();
$db = null;
echo json_encode($location);
} catch (PDEOxception $e) {
echo 'Error';
}
}
Angular JS 代码
//CONTROLLER == add new page
countryApp.controller('AddNew', function($scope, $http, $location) {
$scope.master = {};
$scope.activePath = null;
$scope.add_new = function(location, AddNewForm) {
$http.post('http://localhost/slimtest2/add_location', location).success(function() {
$scope.reset();
$scope.activePath = $location.path('/');
});
$scope.reset = function() {
$scope.location = angular.copy($scope.master);
};
$scope.reset();
};
});
【问题讨论】:
标签: javascript php angularjs rest slim