Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question
    
the error is obvious, google that !!!! – Hadiiiiiiiiiiiiiiiiiiiii Nov 19 '16 at 15:31
up vote 1 down vote accepted

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.

share|improve this answer
    
Still my code is not working . when i am clicking submit button , nothing is showing . – Asif Al Noman Nov 19 '16 at 15:45
    
Turn on the error reporting. @AsifAlNoman and tell me the error – Tirthraj Barot Nov 19 '16 at 15:47
    
Actually i am talking about index.html file . when i am clicking the submit button of index.html file nothing is showing. – Asif Al Noman Nov 19 '16 at 15:53
    
Put an alert and check of the function os being called. See the network tab in developers console in browser.. And see the response obtained in that network tab. @AsifAlNoman – Tirthraj Barot Nov 19 '16 at 15:56
    
And frankly I had been facing this error in the past... I found an alternative function of mysqli_real_escape_string and used it on javascript side and avoided using that function in php.. Things are running as smooth as ever...! :D @AsifAlNoman – Tirthraj Barot Nov 19 '16 at 15:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.