php - Insert data into database in angularjs -
i trying insert data in database not working @ all! followed tutorial , work according not working. tried lot no success till now.
here html file
<!doctype html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"> </script> <body> <div ng-app="myapp" ng-controller="myctrl"> <form> name:-<input type="text" ng-model="bname" /> phone:-<input type="text" ng-model="bphone" /> <input type="button" value="submit" ng-click="insertdata()" /> </form> </div> <script> var app = angular.module('myapp',[]); app.controller('myctrl',function($scope,$http){ $scope.insertdata=function(){ $http.post("insert.php", { 'bname':$scope.bname, 'bphone':$scope.bphone}) .success(function(data,status,headers,config){ console.log("data inserted successfully"); }); } }); </script> </body> </html>
and insert.php in inserting data in database
<?php $data = json_decode(file_get_contents("php://input")); $bname = mysql_real_escape_string($data->bname); $bauthor = mysql_real_escape_string($data->bphone); mysql_connect("localhost", "root", ""); mysql_select_db("angular"); mysql_query("insert ang('name', 'phone') values('".$bname."','".$bauthor."')"); ?>
update
<?php $data = json_decode(file_get_contents("php://input")); $con = new mysqli('localhost', 'root', '', 'angularjs'); $bname = mysqli_real_escape_string($con, $data->bname); $bphone = mysqli_real_escape_string($con, $data->bphone); if($con->connect_errno > 0){ die('unable connect database [' . $con->connect_error . ']'); } mysqli_query($con,"insert jstable (name, phone)values ('".$bname."', '".$bphone."')"); mysqli_close($con); ?>
after updating code got error :-
notice: trying property of non-object in e:\xampp\htdocs\insert.php on line 4
notice: trying property of non-object in e:\xampp\htdocs\insert.php on line 5
i searched regarding error , found many result none of them helpful in case!
i modified code in following manner
html code:
<!doctype html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"> </script> <body> <div ng-app="myapp" ng-controller="myctrl"> <form> name:-<input type="text" ng-model="bname" /> phone:-<input type="text" ng-model="bphone" /> <input type="button" value="submit" ng-click="insertdata()" /> </form> </div> <script> var app = angular.module('myapp',[]); app.controller('myctrl',function($scope,$http){ $scope.insertdata=function(){ $http.post("test.php", { 'bname':$scope.bname, 'bphone':$scope.bphone }).then(function(response){ console.log("data inserted successfully"); },function(error){ alert("sorry! data couldn't inserted!"); console.error(error); }); } }); </script> </body> </html>
and test.php
<?php $data = json_decode(file_get_contents("php://input")); // $bname = mysql_real_escape_string($data->bname); // $bauthor = mysql_real_escape_string($data->bphone); $bname = $data->bname; $bphone = $data->bphone; mysql_connect("localhost", "root", "password"); mysql_select_db("test"); mysql_query("insert `ang`(`name`,`phone`) values ('".$bname."','".$bphone."') ") or die(mysql_error()); echo $bname." ".$bphone; ?>
this works well..
Comments
Post a Comment