angularjs将数据插入到mysql数据库

我正在尝试使用angularjs从前端向mysql db插入数据。 但是,即使没有错误消息,它也不会插入数据库。以下是我使用的代码。

index.html

<html ng-app="demoApp">

<head>

<title> AngularJS Sample</title>

<script src="https://www.coder.work/article/147631/js/angular.min.js"></script>

<script src="https://www.coder.work/article/147631/js/angular-route.min.js"></script>

<script type="text/javascript" src="https://www.coder.work/article/147631/js/script.js"></script>

</head>

<body>

<div class="container" ng-view>

</div>

</body>

</html>

script.js

    demoApp.config( function ($routeProvider,$locationProvider) {

$routeProvider

.when('/',

{

controller: 'SimpleController',

templateUrl: 'Partials/view1.html'

})

.when('/view2',

{

controller: 'SimpleController',

templateUrl: 'Partials/view2.html'

})

.otherwise({redirectTo: '/'});

});

demoApp.controller('SimpleController',function ($scope,$http){

$http.post('server/view.php').success(function(data){

$scope.friends = data;

});;

$scope.addNewFriend = function(add){

var data = {

fname:$scope.newFriend.fname,

lname:$scope.newFriend.lname

}

$http.post("server/insert.php",data).success(function(data, status, headers, config){

console.log("inserted Successfully");

});

$scope.friends.push(data);

$scope.newFriend = {

fname:"",

lname:""

};

};

});

View1.html

<div class="container">

Name:<input type="text" ng-model="filter.name">

<br/>

<ul>

<li ng-repeat="friend in friends | filter:filter.name | orderBy:'fname'">{{friend.fname}} {{friend.lname}}</li>

</ul>

<br/>

<fieldset>

<legend>Add Friend</legend>

<form name="addcustomer" method="POST">

First Name:<input type="text" ng-model="newFriend.fname" name="firstname"/>

<br/>

Last Name :<input type="text" ng-model="newFriend.lname" name="lastname"/>

<br/>

<button data-ng-click="addNewFriend()" name="add">Add Friend</button>

</form>

</fieldset>

<a href="#/view2">Next</a>

</div>

以下是我的php文件

insert.php

<?php

if(isset($_POST['add']))

{

$firsname=$_POST['firstname'];

$laname = $_POST['lastname'];

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("angularjs") or die(mysql_error());

mysql_query("INSERT INTO friends (fname,lname) VALUES ('$firsname', '$laname')");

Print "Your information has been successfully added to the database.";

}

?>

我知道我在这里做一些愚蠢的事情。我今天才刚刚开始学习angularjs。

当我尝试将纯HTML的php代码插入db时,它的工作原理非常完美。希望有人能帮助我

最佳答案

您用于插入数据的脚本是错误的。将其替换为以下内容

$http.post("server/insert.php",{'fstname': $scope.newFriend.fname, 'lstname': $scope.newFriend.lname})

.success(function(data, status, headers, config){

console.log("inserted Successfully");

});

并按照以下步骤更改php。

$data = json_decode(file_get_contents("php://input"));

$fstname = mysql_real_escape_string($data->fstname);

$lstname = mysql_real_escape_string($data->lstname);

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("angularjs") or die(mysql_error());

mysql_query("INSERT INTO friends (fname,lname) VALUES ('$fstname', '$lstname')");

Print "Your information has been successfully added to the database.";

当我尝试使用您的代码时,这对我有用。

以上是 angularjs将数据插入到mysql数据库 的全部内容, 来源链接: www.h5w3.com/122024.html

回到顶部