PHP - Simple Delete MySQLi Data Using AngularJS

In this tutorial we will create a Simple Delete MySQLi Data Using AngularJS. This code will delete a MySQLi data when the user click the delete button. The code use angular directives to to process the http POST request in order to remove a MySQLi data by passing the target id in the ng-click() . This a user-friendly program feel free to modify and use it to your system. We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. Lastly, this is the link for the AngularJS https://angularjs.org/.

Creating Database

Open your database web server then create a database name in it db_del, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn=mysqli_connect("localhost", "root", "", "db_del");
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.php.
  1. <!DOCTYPE html>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4.         <head>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  7.         </head>
  8. <body ng-app="myModule" ng-controller="myController">
  9.         <nav class="navbar navbar-default">
  10.                 <div class="container-fluid">
  11.                         <a class="navbar-brand" href="https://sourcecodester.com" >Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">PHP - Simple Delete MySQLi Data Using AngularJS</h3>
  17.                 <hr style="border-top:1px dotted #ccc;">
  18.                 <div class="col-md-8">
  19.                         <table class = "table table-bordered">
  20.                                 <thead class="alert-info">
  21.                                         <tr>
  22.                                                 <th>Firstname</th>
  23.                                                 <th>Lastname</th>
  24.                                                 <th>Address</th>
  25.                                                 <th>Action</th>
  26.                                         </tr>
  27.                                 </thead>
  28.                                 <tbody style="background-color:#fff;">
  29.                                         <tr ng-repeat = "member in members">
  30.                                                 <td>{{member.firstname}}</td>
  31.                                                 <td>{{member.lastname}}</td>
  32.                                                 <td>{{member.address}}</td>
  33.                                                 <td><button class="btn btn-danger" ng-click="deleteData(member.mem_id)">Delete</button></td>
  34.                                         </tr>
  35.                                 </tbody>
  36.                         </table>
  37.                 </div>
  38.                 <div class="col-md-4">
  39.                         <form>
  40.                                 <div class="form-group">
  41.                                         <label>Firstname</label>
  42.                                         <input type="text" class="form-control" ng-model="firstname"/>
  43.                                 </div>
  44.                                 <div class="form-group">
  45.                                         <label>Lastname</label>
  46.                                         <input type="text" class="form-control" ng-model="lastname"/>
  47.                                 </div>
  48.                                 <div class="form-group">
  49.                                         <label>Address</label>
  50.                                         <input type="text" class="form-control" ng-model="address"/>
  51.                                 </div>
  52.                                 <center><button type = "button" class = "btn btn-primary" ng-click = "saveData()" ><span class = "glyphicon glyphicon-save"></span> Save</button></center>
  53.                         </form>
  54.                 </div>
  55.         </div>
  56. <script src = "js/angular.js"></script>
  57. <script src = "js/script.js"></script>
  58. </body>
  59. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the data inputs in the database server and also retrieve the table at the same time. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below. save_member.php
  1. <?php
  2.         require_once'conn.php';
  3.         $data = json_decode(file_get_contents("php://input"));
  4.        
  5.         $firstname = $data->firstname;
  6.         $lastname = $data->lastname;
  7.         $address = $data->address;
  8.        
  9.         mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  10.        
  11. ?>
data.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         $data = array();
  5.        
  6.         $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  7.        
  8.         while($fetch=mysqli_fetch_array($query)){
  9.                 $data[] = $fetch;
  10.         }
  11.        
  12.         echo json_encode($data);
  13.  
  14. ?>

Creating the Main Function

This code contains the main function of the application. This code will delete a MySQLi data when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below. delete_member.php
  1. <?php
  2.         require_once'conn.php';
  3.         $data = json_decode(file_get_contents("php://input"));
  4.        
  5.         $mem_id = $data->mem_id;
  6.        
  7.        
  8.         mysqli_query($conn, "DELETE FROM `member` WHERE `mem_id`='$mem_id'") or die(mysqli_error());
  9.        
  10. ?>
script.js Note: Make sure you save this file inside the js directory in order the script works.
  1. var app = angular.module("myModule", [])
  2.                                 .controller("myController", function($scope, $http){
  3.                                         $http.get('data.php').then(function(response){
  4.                                                 $scope.members = response.data;
  5.                                         });
  6.                                        
  7.                                         $scope.saveData = function(){
  8.                                                 if($scope.firstname == null || $scope.lastname == null || $scope.address == null){
  9.                                                         alert("Please complete the required field");
  10.                                                 }else{
  11.                                                         $http.post("save_member.php", {firstname: $scope.firstname, lastname: $scope.lastname, address: $scope.address})
  12.                                                         .then(function(){
  13.                                                                 $scope.firstname = "";
  14.                                                                 $scope.lastname = "";
  15.                                                                 $scope.address = "";
  16.                                                                 $scope.getData();
  17.                                                         });    
  18.                                                 }
  19.                                                
  20.                                         }
  21.                                        
  22.                                         $scope.deleteData = function(id){
  23.                                                 $http.post("delete_member.php", {mem_id: id})
  24.                                                         .then(function(){
  25.                                                                 $scope.getData();
  26.                                                         });    
  27.                                         }
  28.                                        
  29.                                        
  30.                                         $scope.getData = function(){
  31.                                                 $http.get('data.php').then(function(response){
  32.                                                         $scope.members = response.data;
  33.                                                 });
  34.                                         }
  35.                                 });
There you have it we successfully created a Simple Delete MySQLi Data using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment