Solomon Solomon - 7 months ago 760
PHP Question

Fetch and insert data into mysql using angularjs

I am unable to connect insert the date into sql server using angularjs & php.

I want to know how to insert data in sql and fetch the data from db.

<body>
<div ng-app="myapp" ng-controller="empcontroller">
<form>
Employe No. <input type="text" ng-model="emp_no" /><br/>
First Name. <input type="text" ng-model="first_name" /><br/>
Last Name. <input type="text" ng-model="last_name" /><br/>
Department. <input type="text" ng-model="dept_name" /><br/>

<input type="button" value="submit" ng-click="insertdata()"/> <br/>
</form>
</div>


<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular-route.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script type="text/javascript">
var app = angular.module('myapp',[]);
app.controller('empcontroller', function($scope, $http){
$scope.insertdata=function(){
$http.post("insert.php",{'emp_no':$scope.emp_no,'first_name':$scope.first_name,'last_name':$scope.last_name,'dept_name':$scope.dept_name})
.success(function(data,status,headers,config){
console.log("data insert succesfully");
});
}
});
</script>
</body>


PHP CODE:

$data = json_decode(file_get_contents("php://input"));
$empno = mysql_real_escape_string($data->emp_no);
$fname = mysql_real_escape_string($data->first_name);
$lname = mysql_real_escape_string($data->last_name);
$dept = mysql_real_escape_string($data->dept_name);

$con = mysql_connect("localhost", "root", "root");
mysql_select_db("company", $con);
mysql_query("INSERT INTO employee('emp_no', 'first_name', 'last_name', 'dept_name')VALUES('".$empno."','".$fname."','".$lname."','".$dept."')");

Answer

Here You go Try this

HTML

<div ng-app="myapp" ng-controller="empcontroller">
        <form>
            Employe No. <input type="text" ng-model="emp_no" /><br/>
            First Name. <input type="text" ng-model="first_name" /><br/>
            Last Name.  <input type="text" ng-model="last_name" /><br/>
            Department. <input type="text" ng-model="dept_name" /><br/>

             <button ng-click="postData()">Submit</button><br>
        </form> 
    </div>

CONTROLLER:

app.controller('empcontroller', function ($scope, $http) {
/*
* This method will be called on click event of button.
*/
$scope.postData = function () {

    var request = $http({
        method: "post",
        url: window.location.href + "insert.php",
        data: {
            emp_no: $scope.emp_no,
            first_name: $scope.first_name,
            last_name: $scope.last_name,
            dept_name: $scope.dept_name,
        },
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    });

 }
}); 

PHP CODE:

<?php

 $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    $emp_no = $request->emp_no;
    $first_name = $request->first_name;
    $last_name = $request->last_name;
    $dept_name = $request->dept_name;


$servername = "localhost";
$username = "root";
$password = "root"; //Your User Password
$dbname = "myDB"; //Your Database Name


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO employee (emp_no, first_name, last_name, dept_name)
VALUES ($emp_no, $first_name, $last_name , $dept_name)";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>