title: Fetch and insert data into mysql using angularjs
body: <p>I am unable to connect insert the date into sql server using angularjs &amp; php.</p>
  
  <p>I want to know how to insert data in sql and fetch the data from db.</p>
  
  <pre><code>&lt;body&gt;
      &lt;div ng-app="myapp" ng-controller="empcontroller"&gt;
          &lt;form&gt;
              Employe No. &lt;input type="text" ng-model="emp_no" /&gt;&lt;br/&gt;
              First Name. &lt;input type="text" ng-model="first_name" /&gt;&lt;br/&gt;
              Last Name.  &lt;input type="text" ng-model="last_name" /&gt;&lt;br/&gt;
              Department. &lt;input type="text" ng-model="dept_name" /&gt;&lt;br/&gt;
  
              &lt;input type="button" value="submit" ng-click="insertdata()"/&gt; &lt;br/&gt;
          &lt;/form&gt; 
      &lt;/div&gt;
  
  
      &lt;script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"&gt;&lt;/script&gt;
      &lt;script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular-route.min.js"&gt;&lt;/script&gt;    
      &lt;script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          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");
                      });
              }
          });
      &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>PHP CODE:</p>
  
  <pre><code>$data = json_decode(file_get_contents("php://input"));
  $empno = mysql_real_escape_string($data-&gt;emp_no);
  $fname = mysql_real_escape_string($data-&gt;first_name);
  $lname = mysql_real_escape_string($data-&gt;last_name);
  $dept = mysql_real_escape_string($data-&gt;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."')");
  </code></pre>