所以我是Angular的新手,并且从未通过Angular与PHP建立连接。我的角度设置很好。数据正在通过,但我似乎无法将其保存到我的数据库
这是我的html代码:
<form ng-controller="AppCtrl" name="add_user">
<div class="modal-header">
<h3 class="modal-title">Add a user by sending an invite via e-mail</h3>
</div>
<div class="modal-body">
<input type="text" class="form-control" name="user_email" ng-model="user_name" placeholder="Enter a name">
<br />
<input type="text" class="form-control" name="user_name" ng-model="user_email" placeholder="Enter an e-mail adress">
</div>
<div class="modal-footer">
<!-- <button type="" class="btn btn-success" ng-click="add_user()">Invite</button> -->
<input type="button" class="btn btn-success" name="add_user" value="Invite" ng-click="save_user()">
<button class="btn btn-warning">Cancel</button>
</div>
</form>
这是我的app.js代码:
var app = angular.module('AddUser', []);
app.controller('AppCtrl', function($scope, $http){
$scope.save_user = function() {
$http.post('db.php?action=add_user',
{
'user_name' : $scope.user_name,
'user_email' : $scope.user_email
}
)
.success(function (data, status, headers, config) {
console.log("The user has been added successfully to the DB");
console.log(data);
})
.error(function(data, status, headers, config) {
console.log("Failed to add the user to DB ");
});
}
});
这是我的PHP代码:
<?php
include('config.php');
//echo ('test' . $_GET['action']);
switch($_GET['action']) {
case 'add_user' :
add_user();
break;
}
/** Function to add user to db **/
function add_user() {
$data = json_decode(file_get_contents("php://input"));
$user_name = $data->user_name;
$user_email = $data->user_email;
print_r($data);
$qry = 'INSERT INTO tblUser(user_name, user_email) VALUES ("' . $user_name . '","' . $user_email . ')';
echo ($qry);
$qry_res = mysql_query($qry);
if ($qry_res) {
$arr = array('msg' => "User added successfully!!!", 'error' => '');
$jsn = json_encode($arr);
// print_r($jsn);
}
else {
$arr = array('msg' => "", 'error' => 'Error in inserting record');
$jsn = json_encode($arr);
// print_r($jsn);
}
}
&GT;
如果有人能指出我正确的方向,我会非常感激
答案 0 :(得分:3)
您在插入SQL上错过了电子邮件中的结束双引号。
应该是;
$qry = 'INSERT INTO tblUser(user_name, user_email) VALUES ("' . $user_name . '","' . $user_email . '")';