Asif Al Noman Asif Al Noman - 21 days ago 7
MySQL Question

Inserting Data in mySql Database using AngularJs

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


Answer

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.