0

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
  • the error is obvious, google that !!!! Commented Nov 19, 2016 at 15:31

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Still my code is not working . when i am clicking submit button , nothing is showing .
Turn on the error reporting. @AsifAlNoman and tell me the error
Actually i am talking about index.html file . when i am clicking the submit button of index.html file nothing is showing.
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
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.