• We just launched and are currently in beta. Join us as we build and grow the community.

Creating an AngularJS CRUD using UI-Router and PHP/MySQLi Tutorial

PinkHamburgers

Payload Specialist
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
168
Likes
51
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This tutorial tackles on how to create a CRUD Operation in Angular JS using Ui-Router to manage our routes and PHP/MySQLi to handle our backend with MySQL as our Database. You will learn in this tutorial how to use UI-Router. Angular JS is a javascript framework maintained by Google and is capable of creating Single-Page Applications.

Getting Started

I've used CDN for Bootstrap, Angular JS, and Ui-Router so you need an internet connection for them to work.

Creating our Database

First, we're going to create our MySQL database.

  1. Open phpMyAdmin
  2. Click databases, create a database and name it as angular


  3. After creating a database, click the SQL

    and paste the below codes. See image below for detailed instruction.

  1. CREATE

    TABLE

    `members`

    (
  2. `memid`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `firstname`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  4. `lastname`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  5. `address`

    text

    NOT

    NULL

    ,
  6. PRIMARY KEY

    (

    `memid`

    )
  7. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

database_6_15.png


Creating the Interface

This is the main view of our app and the place where we declare all our dependencies. Save the file as index.html

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    ng-app=

    "app"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "utf-8"

    >
  5. <title

    >

    AngularJS CRUD using Ui-Router in PHP/MySQLi</

    title

    >
  6. <link

    href

    =

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

    rel

    =

    "stylesheet"

    >
  7. <link

    href

    =

    "https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"

    rel

    =

    "stylesheet"

    >
  8. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"

    ></

    script

    >
  9. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"

    ></

    script

    >
  10. </

    head

    >
  11. <body

    >
  12. <div

    class

    =

    "container"

    >
  13. <h1

    class

    =

    "page-header text-center"

    >

    AngularJS CRUD using Ui-Router in PHP/MySQLi</

    h1

    >
  14. <!-- this is where we render our view -->
  15. <div

    ui-view></

    div

    >
  16. </

    div

    >
  17. <!-- main script -->
  18. <script

    src

    =

    "js/app.js"

    ></

    script

    >
  19. <!-- controllers -->
  20. <script

    src

    =

    "js/controllers/homeController.js"

    ></

    script

    >
  21. <script

    src

    =

    "js/controllers/addController.js"

    ></

    script

    >
  22. <script

    src

    =

    "js/controllers/editController.js"

    ></

    script

    >
  23. <script

    src

    =

    "js/controllers/deleteController.js"

    ></

    script

    >
  24. <!-- services -->
  25. <script

    src

    =

    "js/services/memberService.js"

    ></

    script

    >
  26. </

    body

    >
  27. </

    html

    >

Creating the Angular Scripts

Note: In this Tutorial I compiled all the angular files and directories in the single folder naming js

.

app.js



This is the main angular script of our app.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    'ui.router'

    ]

    )

    ;

  2. app.config

    (

    function

    (

    $stateProvider,

    $urlRouterProvider)

    {

  3. $urlRouterProvider.otherwise

    (

    '/home'

    )

    ;
  4. $stateProvider
  5. .state

    (

    'home'

    ,

    {
  6. url:

    '/home'

    ,
  7. templateUrl:

    'partials/home.html'

    ,
  8. controller:

    'homeCtrl'
  9. }

    )
  10. .state

    (

    'add'

    ,

    {
  11. url:

    '/add'

    ,
  12. templateUrl:

    'partials/add.html'

    ,
  13. controller:

    'addCtrl'
  14. }

    )
  15. .state

    (

    'edit'

    ,

    {
  16. url:

    '/edit/{member:json}'

    ,
  17. templateUrl:

    'partials/edit.html'

    ,
  18. controller:

    'editCtrl'
  19. }

    )
  20. .state

    (

    'delete'

    ,

    {
  21. url:

    '/delete/{member:json}'

    ,
  22. templateUrl:

    'partials/delete.html'

    ,
  23. controller:

    'deleteCtrl'
  24. }

    )

  25. }

    )

    ;

services/memberService.js



This is the service that we created to handle our members and request.

  1. 'use strict'

    ;

  2. app.factory

    (

    'memberService'

    ,

    function

    (

    $http)

    {
  3. return

    {
  4. read:

    function

    (

    )

    {
  5. var

    read =

    $http.get

    (

    'api/read.php'

    )

    ;
  6. return

    read;
  7. }

    ,
  8. create:

    function

    (

    member)

    {
  9. var

    add =

    $http.post

    (

    'api/add.php'

    ,

    member)

    ;
  10. return

    add;
  11. }

    ,
  12. update:

    function

    (

    member)

    {
  13. var

    edit =

    $http.post

    (

    'api/edit.php'

    ,

    member)

    ;
  14. return

    edit;
  15. }

    ,
  16. delete

    :

    function

    (

    member)

    {
  17. var

    del =

    $http.post

    (

    'api/delete.php'

    ,

    member)

    ;
  18. return

    del;
  19. }
  20. }
  21. }

    )

    ;

controller/homeController.js



This is our controller for home.html

.

  1. 'use strict'

    ;

  2. app.controller

    (

    'homeCtrl'

    ,

    [

    '$scope'

    ,

    'memberService'

    ,

    function

    (

    $scope,

    memberService)

    {
  3. //fetch members
  4. $scope.fetch

    =

    function

    (

    )

    {
  5. var

    members =

    memberService.read

    (

    )

    ;
  6. members.then

    (

    function

    (

    response)

    {
  7. $scope.members

    =

    response.data

    ;
  8. }

    )

    ;
  9. }

  10. }

    ]

    )

    ;

controller/addController.js



This the controller for our add.html

.

  1. 'use strict'

    ;

  2. app.controller

    (

    'addCtrl'

    ,

    [

    '$scope'

    ,

    'memberService'

    ,

    '$location'

    ,

    function

    (

    $scope,

    memberService,

    $location)

    {
  3. $scope.error

    =

    false

    ;
  4. //add member
  5. $scope.add

    =

    function

    (

    )

    {
  6. var

    addmember =

    memberService.create

    (

    $scope.member

    )

    ;
  7. addmember.then

    (

    function

    (

    response)

    {
  8. if

    (

    response.data

    .error

    )

    {
  9. $scope.error

    =

    true

    ;
  10. $scope.message

    =

    response.data

    .message

    ;
  11. }
  12. else

    {
  13. console.log

    (

    response)

    ;
  14. $location.path

    (

    'home'

    )

    ;
  15. }
  16. }

    )

    ;
  17. }

  18. }

    ]

    )

    ;

controller/editController.js



This is our controller for edit.html

.

  1. 'use strict'

    ;

  2. app.controller

    (

    'editCtrl'

    ,

    [

    '$scope'

    ,

    'memberService'

    ,

    '$location'

    ,

    '$stateParams'

    ,

    function

    (

    $scope,

    memberService,

    $location,

    $stateParams)

    {
  3. $scope.error

    =

    false

    ;
  4. $scope.updatedmember

    =

    $stateParams.member

    ;

  5. //edit member
  6. $scope.update

    =

    function

    (

    )

    {
  7. var

    updatemember =

    memberService.update

    (

    $scope.updatedmember

    )

    ;
  8. updatemember.then

    (

    function

    (

    response)

    {
  9. console.log

    (

    response)

    ;
  10. if

    (

    response.data

    .error

    )

    {
  11. $scope.error

    =

    true

    ;
  12. $scope.message

    =

    response.data

    .message

    ;
  13. }
  14. else

    {
  15. console.log

    (

    response)

    ;
  16. $location.path

    (

    'home'

    )

    ;
  17. }
  18. }

    )

    ;
  19. }

  20. }

    ]

    )

    ;

controller/deleteController.js



This is our controller for delete.html

.

  1. 'use strict'

    ;

  2. app.controller

    (

    'deleteCtrl'

    ,

    [

    '$scope'

    ,

    'memberService'

    ,

    '$location'

    ,

    '$stateParams'

    ,

    function

    (

    $scope,

    memberService,

    $location,

    $stateParams)

    {
  3. $scope.error

    =

    false

    ;
  4. $scope.deletemember

    =

    $stateParams.member

    ;

  5. //delete member
  6. $scope.delete

    =

    function

    (

    )

    {
  7. var

    dmember =

    memberService.delete

    (

    $scope.deletemember

    )

    ;
  8. dmember.then

    (

    function

    (

    response)

    {
  9. if

    (

    response.data

    .error

    )

    {
  10. $scope.error

    =

    true

    ;
  11. $scope.message

    =

    response.data

    .message

    ;
  12. }
  13. else

    {
  14. console.log

    (

    response)

    ;
  15. $location.path

    (

    'home'

    )

    ;
  16. }
  17. }

    )

    ;
  18. }
  19. }

    ]

    )

    ;

Creating the Interfaces

Note: I compiled the following files inside partials

folder.

home.html



As per our ui-router, this is considered to be our index view.

  1. <div

    class

    =

    "row"

    ng-init=

    "fetch()"

    >
  2. <div

    class

    =

    "col-md-8 col-md-offset-2"

    >
  3. <button

    href

    =

    ""

    class

    =

    "btn btn-primary"

    ui-sref=

    "add"

    ><i

    class

    =

    "fa fa-plus"

    ></

    i

    >

    Add New</

    button

    >
  4. <table

    class

    =

    "table table-bordered table-striped"

    style

    =

    "margin-top:10px;"

    >
  5. <thead

    >
  6. <tr

    >
  7. <th

    >

    Firstname</

    th

    >
  8. <th

    >

    Lastname</

    th

    >
  9. <th

    >

    Address</

    th

    >
  10. <th

    >

    Action</

    th

    >
  11. </

    tr

    >
  12. </

    thead

    >
  13. <tbody

    >
  14. <tr

    ng-repeat=

    "member in members"

    >
  15. <td

    >

    {{ member.firstname }}</

    td

    >
  16. <td

    >

    {{ member.lastname }}</

    td

    >
  17. <td

    >

    {{ member.address }}</

    td

    >
  18. <td

    >
  19. <button

    type

    =

    "button"

    class

    =

    "btn btn-success"

    ui-sref=

    "edit({member: member})"

    ><i

    class

    =

    "fa fa-edit"

    ></

    i

    >

    Edit</

    button

    >
  20. <button

    type

    =

    "button"

    class

    =

    "btn btn-danger"

    ui-sref=

    "delete({member: member})"

    >

    <i

    class

    =

    "fa fa-trash"

    ></

    i

    >

    Delete</

    button

    >
  21. </

    td

    >
  22. </

    tr

    >
  23. </

    tbody

    >
  24. </

    table

    >
  25. </

    div

    >
  26. </

    div

    >

add.html



This contains the add new form.

  1. <div

    class

    =

    "row"

    >
  2. <div

    class

    =

    "col-sm-4 col-sm-offset-4"

    >
  3. <div

    class

    =

    "alert alert-danger text-center"

    ng-show=

    "error"

    >
  4. <button

    type

    =

    "button"

    class

    =

    "close"

    ng-click=

    "clear()"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  5. <i

    class

    =

    "fa fa-warning"

    ></

    i

    >

    {{ message }}
  6. </

    div

    >
  7. <div

    class

    =

    "panel panel-default"

    >
  8. <div

    class

    =

    "panel-body"

    >
  9. <h3

    class

    =

    "text-center"

    >

    Add Form</

    h3

    >
  10. <div

    class

    =

    "form-group"

    >
  11. <label

    >

    Firstname:</

    label

    >
  12. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "member.firstname"

    >
  13. </

    div

    >
  14. <div

    class

    =

    "form-group"

    >
  15. <label

    >

    Lastname:</

    label

    >
  16. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "member.lastname"

    >
  17. </

    div

    >
  18. <div

    class

    =

    "form-group"

    >
  19. <label

    >

    Address:</

    label

    >
  20. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "member.address"

    >
  21. </

    div

    >
  22. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    ng-click=

    "add()"

    ><i

    class

    =

    "fa fa-save"

    ></

    i

    >

    Save</

    button

    >
  23. <button

    type

    =

    "button"

    class

    =

    "btn btn-default pull-right"

    ui-sref=

    "home"

    ><i

    class

    =

    "fa fa-arrow-left"

    ></

    i

    >

    Back</

    button

    >
  24. </

    div

    >
  25. </

    div

    >
  26. </

    div

    >
  27. </

    div

    >

edit.html



This contains our edit form.

  1. <div

    class

    =

    "row"

    >
  2. <div

    class

    =

    "col-sm-4 col-sm-offset-4"

    >
  3. <div

    class

    =

    "alert alert-danger text-center"

    ng-show=

    "error"

    >
  4. <button

    type

    =

    "button"

    class

    =

    "close"

    ng-click=

    "clear()"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  5. <i

    class

    =

    "fa fa-warning"

    ></

    i

    >

    {{ message }}
  6. </

    div

    >
  7. <div

    class

    =

    "panel panel-default"

    >
  8. <div

    class

    =

    "panel-body"

    >
  9. <h3

    class

    =

    "text-center"

    >

    Edit Form</

    h3

    >
  10. <div

    class

    =

    "form-group"

    >
  11. <label

    >

    Firstname:</

    label

    >
  12. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "updatedmember.firstname"

    >
  13. </

    div

    >
  14. <div

    class

    =

    "form-group"

    >
  15. <label

    >

    Lastname:</

    label

    >
  16. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "updatedmember.lastname"

    >
  17. </

    div

    >
  18. <div

    class

    =

    "form-group"

    >
  19. <label

    >

    Address:</

    label

    >
  20. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "updatedmember.address"

    >
  21. </

    div

    >
  22. <button

    type

    =

    "button"

    class

    =

    "btn btn-success"

    ng-click=

    "update()"

    ><i

    class

    =

    "fa fa-check-square-o"

    ></

    i

    >

    Update</

    button

    >
  23. <button

    type

    =

    "button"

    class

    =

    "btn btn-default pull-right"

    ui-sref=

    "home"

    ><i

    class

    =

    "fa fa-arrow-left"

    ></

    i

    >

    Back</

    button

    >
  24. </

    div

    >
  25. </

    div

    >
  26. </

    div

    >
  27. </

    div

    >

delete.html



This is delete confirmation view.

  1. <div

    class

    =

    "row"

    >
  2. <div

    class

    =

    "col-sm-4 col-sm-offset-4"

    >
  3. <div

    class

    =

    "alert alert-danger text-center"

    ng-show=

    "error"

    >
  4. <button

    type

    =

    "button"

    class

    =

    "close"

    ng-click=

    "clear()"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  5. <i

    class

    =

    "fa fa-warning"

    ></

    i

    >

    {{ message }}
  6. </

    div

    >
  7. <div

    class

    =

    "panel panel-default"

    >
  8. <div

    class

    =

    "panel-body"

    >
  9. <h4

    class

    =

    "text-center"

    >

    Are you sure you want to delete</

    h4

    >
  10. <h3

    class

    =

    "text-center"

    >

    {{ deletemember.firstname + ' ' + deletemember.lastname }}</

    h3

    ><br

    >
  11. <button

    type

    =

    "button"

    class

    =

    "btn btn-danger"

    ng-click=

    "delete()"

    ><i

    class

    =

    "fa fa-trash"

    ></

    i

    >

    Delete</

    button

    >
  12. <button

    type

    =

    "button"

    class

    =

    "btn btn-default pull-right"

    ui-sref=

    "home"

    ><i

    class

    =

    "fa fa-arrow-left"

    ></

    i

    >

    Back</

    button

    >
  13. </

    div

    >
  14. </

    div

    >

  15. </

    div

    >
  16. </

    div

    >

Creating the PHP API Scripts

Note: I compiled the following files inside api

folder.

read.html



This is our PHP API the fetches data from our database.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $out

    =

    array

    (

    )

    ;
  4. $sql

    =

    "SELECT * FROM members"

    ;
  5. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  6. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  7. $out

    [

    ]

    =

    $row

    ;
  8. }

  9. echo

    json_encode

    (

    $out

    )

    ;
  10. ?>

add.html



Our PHP API that adds new data to our database.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $data

    =

    json_decode

    (

    file_get_contents

    (

    "php://input"

    )

    )

    ;

  4. $out

    =

    array

    (

    'error'

    =>

    false

    ,

    'firstname'

    =>

    false

    ,

    'lastname'

    =>

    false

    ,

    'address'

    =>

    false

    )

    ;

  5. $firstname

    =

    $data

    ->

    firstname

    ;
  6. $lastname

    =

    $data

    ->

    lastname

    ;
  7. $address

    =

    $data

    ->

    address

    ;

  8. if

    (

    empty

    (

    $firstname

    )

    )

    {
  9. $out

    [

    'firstname'

    ]

    =

    true

    ;
  10. $out

    [

    'message'

    ]

    =

    'Firstname is required'

    ;
  11. }
  12. elseif

    (

    empty

    (

    $lastname

    )

    )

    {
  13. $out

    [

    'lastname'

    ]

    =

    true

    ;
  14. $out

    [

    'message'

    ]

    =

    'Lastname is required'

    ;
  15. }
  16. elseif

    (

    empty

    (

    $address

    )

    )

    {
  17. $out

    [

    'address'

    ]

    =

    true

    ;
  18. $out

    [

    'message'

    ]

    =

    'Address is required'

    ;
  19. }
  20. else

    {
  21. $sql

    =

    "INSERT INTO members (firstname, lastname, address) VALUES ('$firstname

    ', '$lastname

    ', '$address

    ')"

    ;
  22. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  23. if

    (

    $query

    )

    {
  24. $out

    [

    'message'

    ]

    =

    'Member Added Successfully'

    ;
  25. }
  26. else

    {
  27. $out

    [

    'error'

    ]

    =

    true

    ;
  28. $out

    [

    'message'

    ]

    =

    'Cannot Add Member'

    ;
  29. }
  30. }

  31. echo

    json_encode

    (

    $out

    )

    ;
  32. ?>

edit.html



Our PHP API updates our selected data.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $data

    =

    json_decode

    (

    file_get_contents

    (

    "php://input"

    )

    )

    ;

  4. $out

    =

    array

    (

    'error'

    =>

    false

    )

    ;

  5. $firstname

    =

    $data

    ->

    firstname

    ;
  6. $lastname

    =

    $data

    ->

    lastname

    ;
  7. $address

    =

    $data

    ->

    address

    ;
  8. $memid

    =

    $data

    ->

    memid

    ;

  9. $sql

    =

    "UPDATE members SET firstname = '$firstname

    ', lastname = '$lastname

    ', address = '$address

    ' WHERE memid = '$memid

    '"

    ;
  10. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  11. if

    (

    $query

    )

    {
  12. $out

    [

    'message'

    ]

    =

    'Member updated Successfully'

    ;
  13. }
  14. else

    {
  15. $out

    [

    'error'

    ]

    =

    true

    ;
  16. $out

    [

    'message'

    ]

    =

    'Cannot update Member'

    ;
  17. }

  18. echo

    json_encode

    (

    $out

    )

    ;
  19. ?>

delete.html



Lastly, this is our PHP API that deletes our selected data.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $data

    =

    json_decode

    (

    file_get_contents

    (

    "php://input"

    )

    )

    ;

  4. $out

    =

    array

    (

    'error'

    =>

    false

    )

    ;

  5. $memid

    =

    $data

    ->

    memid

    ;

  6. $sql

    =

    "DELETE FROM members WHERE memid = '$memid

    '"

    ;
  7. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  8. if

    (

    $query

    )

    {
  9. $out

    [

    'message'

    ]

    =

    'Member deleted Successfully'

    ;
  10. }
  11. else

    {
  12. $out

    [

    'error'

    ]

    =

    true

    ;
  13. $out

    [

    'message'

    ]

    =

    'Cannot delete Member'

    ;
  14. }

  15. echo

    json_encode

    (

    $out

    )

    ;
  16. ?>

DEMO

That's it! You can now test the Simple CRUD Web App using Angular JS and PHP in your browser. I hope this tutorial will help you to understand how to create a CRUD application in Angular JS and PHP and also for your future Web Application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

440,010

316,559

316,568

Top