title: Insert data into database in angularjs
body: <p>I am trying to insert data in database but its not working at all! I followed a tutorial and work according to it But its not working. I tried a lot but no success till now.</p>
  <p><strong>Here is my html file</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;script  src=&quot;http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js&quot;&gt;  &lt;/script&gt;
  &lt;body&gt;
  &lt;div ng-app=&quot;myApp&quot; ng-controller=&quot;myCtrl&quot;&gt;
  &lt;form&gt;
  Name:-&lt;input type=&quot;text&quot; ng-model=&quot;bname&quot; /&gt;
  Phone:-&lt;input type=&quot;text&quot; ng-model=&quot;bphone&quot; /&gt;
  &lt;input type=&quot;button&quot; value=&quot;Submit&quot; ng-click=&quot;insertData()&quot; /&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  &lt;script&gt;
   var app = angular.module('myApp',[]);
   app.controller('myCtrl',function($scope,$http){    
   $scope.insertData=function(){      
   $http.post(&quot;insert.php&quot;, {
      'bname':$scope.bname,
      'bphone':$scope.bphone})        
  
   .success(function(data,status,headers,config){
   console.log(&quot;Data Inserted Successfully&quot;);
   });
      }
       });
       &lt;/script&gt;
       
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>And insert.php in which I am inserting data in database</strong></p>
  <pre><code>&lt;?php 
  $data = json_decode(file_get_contents(&quot;php://input&quot;));
  $bname = mysql_real_escape_string($data-&gt;bname);
  $bauthor = mysql_real_escape_string($data-&gt;bphone);
  mysql_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;); 
  mysql_select_db(&quot;angular&quot;);
  mysql_query(&quot;INSERT INTO ang('name', 'phone')    VALUES('&quot;.$bname.&quot;','&quot;.$bauthor.&quot;')&quot;);
  ?&gt;
  </code></pre>
  <p><strong>UPDATE</strong></p>
  <pre><code>    &lt;?php 
  $data = json_decode(file_get_contents(&quot;php://input&quot;));
  $con = new mysqli('localhost', 'root', '', 'angularjs');
  $bname = mysqli_real_escape_string($con, $data-&gt;bname);
  $bphone = mysqli_real_escape_string($con, $data-&gt;bphone);
  if($con-&gt;connect_errno &gt; 0){
    die('Unable to connect to database [' . $con-&gt;connect_error . ']');
  }
  mysqli_query($con,&quot;INSERT INTO jstable (name, phone)VALUES ('&quot;.$bname.&quot;', '&quot;.$bphone.&quot;')&quot;);    
  mysqli_close($con);
  ?&gt;
  </code></pre>
  <p>After Updating my code I got this error :-</p>
  <blockquote>
  <p>Notice: Trying to get property of non-object in E:\xampp\htdocs\insert.php on line 4</p>
  <p>Notice: Trying to get property of non-object in E:\xampp\htdocs\insert.php on line 5</p>
  </blockquote>
  <p>I searched regarding this error and found many result but none of them was helpful in my case!</p>