Recupera e inserisci i dati in mysql usando angularjs

Non riesco a connetterti inserisci la data nel server sql usando angularjs e php.

Voglio sapere come inserire i dati in sql e recuperare i dati da 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> 

CODICE PHP:

 $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."')"); 

Ecco qua Prova questo

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' } }); } }); 

CODICE PHP:

 <?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(); ?> 

Tre problemi con il tuo codice:

  1. Quando si eseguono le funzioni $scope con ng-click, è meglio passare le variables mentre le si utilizza.
  2. Poiché il tuo controller PHP è in attesa di JSON, devi creare un object JSON e indicarlo nelle intestazioni.
  3. .success() è in fase di deprecazione. Dovresti invece usare la promise. .then() .

HTML:

 <!-- need to pass model in the ng-click function --> <input type="button" value="submit" ng-click="insertdata(emp_no, first_name, last_name, dept_name)"/> 

controller:

 $scope.insertata = function(empNo, firstName, lastName, deptName) { //make json payload object var payload = { emp_no: empNo, first_name: firstName, last_name: lastName, dept_name: deptName }; //pass to API $http.post('insert.php', payload, { headers: { 'Content-Type': 'application/json; charset=utf-8' } }).then(function(data, status, headers, config) { //success }, function(data, status, headers, config) { //an error occurred }); } 

bene, usando il codice di KKKKKKKK ora hai bisogno di un codice php.

Per recuperare informazioni da un file JSON pubblicato tramite post su php devi fare qualcosa del genere:

 $json = file_get_contents('php://input'); $obj = json_decode($json); // this will retrieve the json. 

E ora manipola come vuoi.