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

<!doctype html>
<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

<?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

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

Was this article helpful?

Yes No
×

How can we improve it?

×

We appreciate your helpul feedback!

Your answer will be used to improve our content. The more feedback you give us, the better our pages can be.

Follow us on social media:

Facebook Pinterest
Share via:

Mraj

Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts.

Leave a Reply