Getting Started
Please take note that CSS and angular.js used in this tutorial are hosted so you need internet connection for them to work.
Creating our Database
1. Open phpMyAdmin.
2. Click databases, create a database and name it as
crud.
3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
CREATE TABLE `members` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`lastname` VARCHAR(50) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Creating our Connection
Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as
conn.php.
<?php
$conn = new mysqli("localhost", "root", "", "crud");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }
?>
index.php
This is our
Read which contains our Member Table.
<!DOCTYPE html>
<html ng-app="mem_app" ng-controller="controller" ng-init="showdata()"> <title>PHP CRUD Operation using angular.js V2
</title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <h1 class="page-header text-center">PHP CRUD using angular.js V2
</h1> <div class="col-md-10 col-md-offset-1"> <a href="#addnew" data-toggle="modal" class="btn btn-primary pull-right" ng-click="insert()"><i class="fa fa-plus"></i> Member
</a> <table class="table table-bordered table-striped"> <tr ng-repeat="mem in member"> <input type="hidden" value="{{mem.id}}"> <td>{{mem.firstname}}
</td> <td>{{mem.lastname}}
</td> <a href="#addnew" data-toggle="modal" class="btn btn-success" ng-click="update(mem.id, mem.firstname, mem.lastname)"><i class="fa fa-edit"></i> Edit
</a> ||
<a href="#delete" data-toggle="modal" class="btn btn-danger" ng-click="delmem(mem.id)"><i class="fa fa-trash"></i> Delete
</a> <?php include('modal.php'); ?>
myangular.js
This is our angular script which contains all our angular functions.
var app = angular.module("mem_app", []);
app.controller("controller", function($scope, $http) {
$scope.action = function() {
if ($scope.firstname == null) {
alert("Please input Firstname");
}
else if ($scope.lastname == null) {
alert("Please input Lastname");
}
else {
$http.post(
"action.php", {
'firstname': $scope.firstname,
'lastname': $scope.lastname,
'btnName': $scope.btnName,
'memberid': $scope.memberid,
}
).success(function() {
$scope.firstname = null;
$scope.lastname = null;
$scope.btnName = "Save";
$scope.icon = "fa fa-save";
$scope.btnClass = "btn btn-primary";
$scope.showdata();
});
}
}
$scope.showdata = function() {
$http.get("fetch.php")
.success(function(data) {
$scope.member = data;
});
}
$scope.insert = function() {
$scope.firstname = null;
$scope.lastname = null;
$scope.title = "Add New Member";
$scope.btnName = "Save";
$scope.icon = "fa fa-save";
$scope.btnClass = "btn btn-primary";
}
$scope.update = function(memberid, firstname, lastname) {
$scope.memberid = memberid;
$scope.firstname = firstname;
$scope.lastname = lastname;
$scope.title = "Edit Member";
$scope.icon = "fa fa-check-square-o";
$scope.btnClass = "btn btn-success";
$scope.btnName = "Update";
}
$scope.delmem = function(memberid) {
$scope.delmember = memberid;
}
$scope.delete = function() {
$http.post("delete.php", {
'memberid': $scope.delmember,
}
).success(function(data) {
$scope.showdata();
});
}
$scope.enter = function(keyEvent) {
if (keyEvent.which === 13){
action();
}
}
});
fetch.php
This is our PHP code in fetching our table data.
<?php
include('conn.php');
$query = $conn->query("select * from members");
if ($query->num_rows > 0) {
while ($row = $query->fetch_array()) {
$output[] = $row;
}
}
?>
modal.php
This contains our 3 modals which are add modal, edit modal and delete modal.
<!-- Add/Edit Modal -->
<div class="modal fade" id="addnew" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title text-center" id="myModalLabel">{{ title }}
</h4> <form ng-submit="myFunc()"> <div class="col-md-2" style="margin-top:7px;"> <input type="text" class="form-control" ng-model="firstname" placeholder="Enter Firstname"> <div class="col-md-2" style="margin-top:7px;"> <input type="text" class="form-control" ng-model="lastname" placeholder="Enter Lastname"> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> Cancel
</button> <button type="submit" class="{{ btnClass }}" ng-click="action()" data-dismiss="modal"><i class="{{ icon }}"></i> {{ btnName }}
</button>
<!-- Delete Modal -->
<div class="modal fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title text-center" id="myModalLabel">Delete Member
</h4> <h4 class="text-center">Are you sure you want to delete Member?
</h4> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> Cancel
</button> <button type="button" class="btn btn-danger" ng-click="delete()" data-dismiss="modal"><i class="fa fa-trash"></i> Delete
</button>
action.php
This is our PHP code in adding and editing.
<?php
include('conn.php');
$btn_name = $data->btnName;
if ($btn_name == "Save") {
$conn->query("insert into members (firstname, lastname) values ('$firstname', '$lastname')");
}
if ($btn_name == "Update") {
$memberid = $data->memberid;
$conn->query("update members set firstname='$firstname', lastname='$lastname' where id='$memberid'");
}
}
?>
delete.php
Lastly, this is PHP delete code.
<?php
include('conn.php');
$id = $data->memberid;
$conn->query("delete from members where id='$id'");
?>
That ends this tutorial. If you have any comment, suggestion or question, feel free to write it below or message me. Happy Coding :)