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 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?