javascript - 在angularjs中将数据从前端插入mysql db

标签 javascript php mysql angularjs

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

index.html

<html ng-app="demoApp">
   <head>
        <title> AngularJS Sample</title>
        <script src="js/angular.min.js"></script>
        <script src="js/angular-route.min.js"></script>
        <script type="text/javascript" src="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" style="margin:0px 100px 0px 500px;">
    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 style="width:200px;">
        <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" style="margin:auto;">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 代码插入数据库时​​,它工作得很好。我没有明白我在这里做错了什么。希望有人能帮我解决这个问题

最佳答案

您插入数据的脚本有误。将其替换为以下内容

$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."; 

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

关于javascript - 在angularjs中将数据从前端插入mysql db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20004325/

相关文章:

javascript - AngularJS从索引到详细页面的动态路由

javascript - 接收和解释内容类型为应用程序/八位字节流的十六进制数据发送时出错

php - PHP 中的类型转换 - 安全和效率

php - 如何在 Zend Framework 2 中添加 Ajax 请求

php - 相同和不同表中不同 id 的多个联接

javascript - 如何根据值的小数点更改 .toFixed()?

javascript - 测试数组中超过某个值的所有值

PHP:在类构造函数中使用 file_get_contents() 是一种不好的做法吗?

php - 从具有相同列的两个表中选择

mysql - Rails/MySQL 查询,其中第三级连接 ID 不存在