热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在angularjs中从前端插入数据到mysqldb-Insertingdatafromfrontendtomysqldbinangularjs

Iamtryingtoinsertdatafromfrontendtomysqldbusingangularjs.Butitisnotgetinginserted

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.

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

index.html

index . html


   
        
        
        
        
    
    
        

script.js

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

View1.html

Name:
  • {{friend.fname}} {{friend.lname}}

Add Friend First Name:
Last Name :
Next

and following is my php file

下面是我的php文件

insert.php

insert.php

 

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

我知道我在做傻事。我今天刚开始学英语。当我尝试使用纯html的php代码插入到db中时,它会非常有效。我在这里做错了。希望有人能帮我。

1 个解决方案

#1


17  

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.

并更改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."; 

This worked for me when I tried with your code.

当我尝试使用你的代码时,这对我很有效。


推荐阅读
author-avatar
木棉
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有