duangang79177 2016-11-19 15:20
浏览 76
已采纳

使用AngularJs在mySql数据库中插入数据

I am trying to insert some data in mysql database server using AngularJs with PHP/MySQL.But it's not working.Nothing is showing.Why it's not working ? My codes -

index.html

<!DOCTYPE html>
<html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">    </script>

</head>

<body ng-app="myApp" ng-controller="empcontroller">

 <form>

Employee Name:-<input type="text" ng-model="uname" />
Password:-<input type="Password" ng-model="pass" />
<input type="button" value="Submit" ng-click="postData()" />

</form>

<script src="app.js"></script>

 </body>

</html>

app.js

var app = angular.module('myApp', []);

app.controller('empcontroller', function ($scope, $http) {
$scope.postData = function (post) {
var data = {
uname:$scope.empcontroller.uname,
pass:$scope.empcontroller.pass
}
$http.post("http://localhost/query.php", { 'uname':     $scope.empcontroller.uname, 'pass': $scope.empcontroller.pass })
.success(function (data, status, headers, config) {
    console.log($scope.empcontroller.uname);
});
};   
});

query.php

<?php 
header('Access-Control-Allow-Origin: *');
header("Content-Type: application/json; charset=UTF-8");

$mysql_host = "localhost";
$mysql_database = "FirstDB";
$mysql_user = "root";
$mysql_password = "";
// Create connection
$conn = new mysqli($mysql_host, $mysql_user,    $mysql_password,$mysql_database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
$data = json_decode(file_get_contents("php://input"));
$uname = mysql_real_escape_string($data->uname);
$pass = mysql_real_escape_string($data->pass);

$sql = "INSERT INTO Employee (Username,Password) VALUES ('$uname','$pass')";
$result = $conn->query($sql);
echo($uname);
error_log($uname);
$conn->close();

?>

When I am trying to run the php file (query.php) in my localhost server - it gives me the following error -


Fatal error: Uncaught Error: Call to undefined function >mysql_real_escape_string() in I:\xampp\htdocs\query.php:15 Stack trace:

0 {main}

thrown in I:\xampp\htdocs\query.php on line 15

  • 写回答

1条回答 默认 最新

  • duanha3539 2016-11-19 15:30
    关注

    It should be mysqli_real_escape_string

    `i` is missing.
    

    UPDATE

    According to me, your approach can be modified a bit. The error you are getting about the uname not found, that could be corrected but according to me, when you are using AngularJS, you can go more json based.

    The form could be like,

    <form ng-init="obj={}">
    
    Employee Name:-<input type="text" ng-model="obj.uname" />
    Password:-<input type="Password" ng-model="obj.pass" />
    <input type="button" value="Submit" ng-click="postData(obj)" />
    
    </form>
    

    Hence now you have your object to be passed from view to controller as a function parameter.

    Now in the controller,

    app.controller('empcontroller', function ($scope, $http) {
        $scope.postData = function (obj) {
    //NOTE THAT WE DON'T HAVE TO MAKE A NEW OBJECT TO PASS IN POST IF THIS APPROACH IS FOLLOWED.
     //HENCE, OUR OBJECT IS THE SAME WHICH WE PASSED FROM VIEW.
            $http.post("http://localhost/query.php",obj).then(function (response) {
                console.log(response.data);
            },function(error){
                console.error("Error : ",error);
            });
        };   
    });
    

    So you will get your $data in php script exactly as you want.

    Moreover, you can make a factory or service for such http calls.

    This is the approach I suggest, following the best practices.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)