我正在尝试将数据插入mySql数据库但是失败了,我已经尝试了几个小时但没有运气请帮助我。 错误控制台表示angular.js:12011 POST http://localhost:8000/insert.php 405(Method Not Allowed)。 我正在使用Xampp。 这是我的代码
view1.html
<div ng-app="myApp.view1" ng-controller="empcontroller">
<form>
Employe No. <input type="text" ng-model="fName" /><br/>
First Name. <input type="text" ng-model="lName" /><br/>
Last Name. <input type="text" ng-model="eMail" /><br/>
Department. <input type="text" ng-model="userName" /><br/>
Department. <input type="text" ng-model="passWord" /><br/>
Department. <input type="text" ng-model="tel" /><br/>
<button ng-click='SignUp();' >Submit</button><br>
</form>
</div>
view1.js
'use strict';
angular.module('myApp.view1', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {
templateUrl: 'view1/view1.html',
controller: 'empcontroller'
});
}])
.controller('empcontroller', function ($scope, $http) {
$scope.errors = [];
$scope.msgs = [];
$scope.SignUp = function() {
$scope.errors.splice(0, $scope.errors.length); // remove all error messages
$scope.msgs.splice(0, $scope.msgs.length);
$http.post('insert.php', {'fName': $scope.fName,
'lName': $scope.lName,
'Lastname': $scope.last_name,
'eMail': $scope.eMail,
'userName': $scope.userName,
'passWord': $scope.passWord,
'tel': $scope.eMail}
).success(function(data, status, headers, config) {
if (data.msg != '')
{
$scope.msgs.push(data.msg);
}
else
{
$scope.errors.push(data.error);
}
}).error(function(data, status) { // called asynchronously if an error occurs
// or server returns response with an error status.
$scope.errors.push(status);
});
}
});
insert.php
<?php
$data = json_decode(file_get_contents("php://input"));
$request = json_decode($postdata);
$fName = mysql_real_escape_string($data->fName);
$lName= mysql_real_escape_string($data->lName);
$eMail = mysql_real_escape_string($data->eMail);
$userName = mysql_real_escape_string($data->userName);
$passWord= mysql_real_escape_string($data->passWord);
$tel = mysql_real_escape_string($data->tel);
$con = mysql_connect('localhost', 'root', '');
mysql_select_db('MiddleWork', $con);
$qry_em = 'select count(*) as cnt from UserTest where Username ="' . $userName . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);
if ($res['cnt'] == 0) {
$qry = 'INSERT INTO UserTest (Username,Password,Firstname,Lastname,Email,Tel) values ("' . $userName . '","' . $passWord . '","' . $fName . '","' . $lName . '","' . $eMail . '","' . $tel . '")';
$qry_res = mysql_query($qry);
if ($qry_res) {
$arr = array('msg' => "User Created 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);
}
} else {
$arr = array('msg' => "", 'error' => 'User Already exists with same email');
$jsn = json_encode($arr);
print_r($jsn);
}
?>
答案 0 :(得分:-1)
尝试:
labda event, t=self.thumb: self,clikc(t)
在你的php文件中:
$http({
method: 'POST',
url: 'insert.php',
data: {
fName: $scope.fName,
lName: $scope.lName,
Lastname: $scope.last_name,
eMail: $scope.eMail,
userName: $scope.userName,
passWord: $scope.passWord,
tel: $scope.eMail
}
}).success(function(data){
})