March 27, 2017
Delete Data from MySQL Database Using AngularJS with PHP

Hi, friends in this post we going to learn how to delete data from MySQL database using AngularJS with PHP. If you are going to work with a web application using AngularJS & PHP on that you need to delete the data. So how can you delete the data using AngularJS with PHP? In this, post you can find how to delete the data. If you inserted some data into the database that you need to remove without page refresh, So using AngularJS we can do this very easily. The following example will explain you clearly.
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<html> <head> <meta charset="UTF-8"> <title>Softaox | Delete Data from MySQL Database Using AngularJS with PHP</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> </head> <body> <div class="container"> <h3 align="center">Delete Data from MySQL Database Using AngularJS with PHP</h3> <div ng-app="sa_delete" ng-controller="controller" ng-init="show_data()"> <table class="table table-bordered"> <tr> <th>S.No</th> <th>Name</th> <th>Email ID</th> <th>Age</th> <th>Delete</th> </tr> <tr ng-repeat="x in names"> <td>{{x.id}}</td> <td>{{x.name}}</td> <td>{{x.email}}</td> <td>{{x.age}}</td> <td align="center"> <button ng-click="delete_data(x.id )"class="btn btn-danger">X</button> </td> </tr> </table> </div> </div> <!-- Script --> <script> var app = angular.module("sa_delete", []); app.controller("controller", function($scope, $http) { $scope.show_data = function() { $http.get("display.php") .success(function(data) { $scope.names = data; }); } $scope.delete_data = function(id) { if (confirm("Are you sure you want to delete?")) { $http.post("delete.php", { 'id': id }) .success(function(data) { alert(data); $scope.show_data(); }); } else { return false; } } }); </script> </body> </html> |
display.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $conn = mysqli_connect("localhost", "root", "", "angularjs"); $output = array(); $query = "SELECT * FROM insert_emp_info"; $result = mysqli_query($conn, $query); if(mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_array($result)) { $output[] = $row; } echo json_encode($output); } ?> |
delete.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $conn = mysqli_connect("localhost", "root", "", "angularjs"); $info = json_decode(file_get_contents("php://input")); if(count($info) > 0) { $id = $info->id; $query = "DELETE FROM insert_emp_info WHERE id='$id'"; if(mysqli_query($conn, $query)) { echo 'You data has successfully been deleted..'; } else { echo 'Failed'; } } ?> |
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]