在angularjs中从前端插入数据到mysql db [英] Inserting data from front end to mysql db in angularjs

查看:25
本文介绍了在angularjs中从前端插入数据到mysql db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

index.html

<头><标题>AngularJS 示例<script src="js/angular.min.js"></script><script src="js/angular-route.min.js"></script><script type="text/javascript" src="js/script.js"></script><身体><div class="container" ng-view>

</html>

script.js

 demoApp.config( function ($routeProvider,$locationProvider) {$routeProvider.什么时候('/',{控制器:'简单控制器',templateUrl: 'Partials/view1.html'}).when('/view2',{控制器:'简单控制器',templateUrl: 'Partials/view2.html'}).otherwise({redirectTo: '/'});});demoApp.controller('SimpleController',function ($scope,$http){$http.post('server/view.php').success(function(data){$scope.friends = 数据;});;$scope.addNewFriend = 函数(添加){变量数据 = {fname:$scope.newFriend.fname,lname:$scope.newFriend.lname}$http.post("server/insert.php",data).success(function(data, status, headers, config){console.log("插入成功");});$scope.friends.push(data);$scope.newFriend = {fname:"",姓名:""};};});

View1.html

<div class="container" style="margin:0px 100px 0px 500px;">名称:<input type="text" ng-model="filter.name"><br/><ul><li ng-repeat="friend infriends | filter:filter.name | orderBy:'fname'">{{friend.fname}} {{friend.lname}}</li><br/><fieldset style="width:200px;"><legend>添加好友</legend><form name="addcustomer" method="POST">名字:<input type="text" ng-model="newFriend.fname" name="firstname"/><br/>姓氏:<input type="text" ng-model="newFriend.lname" name="lastname"/><br/><button data-ng-click="addNewFriend()" name="add">添加好友</button></表单></fieldset><a href="#/view2" style="margin:auto;">下一步</a>

以下是我的php文件

insert.php

我知道我在这里做了一些愚蠢的事情.我今天刚刚开始学习angularjs.当我尝试将带有纯 html 的 php 代码插入到 db 时,它运行良好.我没有得到我在这里做错的事情.希望有人能帮助我

解决方案

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

$http.post("server/insert.php",{'fstname': $scope.newFriend.fname, 'lstname': $scope.newFriend.lname}).成功(功能(数据,状态,标题,配置){console.log("插入成功");});

并且也改变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", "") 或 die(mysql_error());mysql_select_db("angularjs") 或 die(mysql_error());mysql_query("INSERT INTO Friends (fname,lname) VALUES ('$fstname', '$lstname')");打印您的信息已成功添加到数据库中.";

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

I am trying to insert data from front end to mysql db using angularjs. But it is not geting inserted to the db even though there are no error messages. Following is the code that I use.

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>

and following is my php file

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."; 
    }
?> 

I know I am doing something stupid here. I have just started to learn angularjs today. when i try the php code with plain html to insert into db it works perfectly.I am not getting what I am doing wrong here. Hope someone will help me out here

解决方案

Your script for inserting the data is wrong. Replace it with the following

$http.post("server/insert.php",{'fstname': $scope.newFriend.fname, 'lstname': $scope.newFriend.lname})
        .success(function(data, status, headers, config){
            console.log("inserted Successfully");
        });

and also change the php as follows.

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

This worked for me when I tried with your code.

这篇关于在angularjs中从前端插入数据到mysql db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆