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条回答 默认 最新

相关推荐 更多相似问题