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

PHP - Simple CRUD With Ajax/MySQLi

cracker16

Couch Co-Op King
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
110
Likes
110
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Simple CRUD With Ajax/MySQLi using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. Using Ajax, data could then be passed between the browser and the server, using the XMLHttpRequest API, without having to reload the web page. So Let's do the coding.

Before we 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 jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/
Creating Database

Open your database web server then create a database name in it db_crud, after that click Import then locate the database file inside the folder of the application then click ok.
2018-06-08_15_04_31-localhost_127.0.0.1_db_crud_phpmyadmin_4.7.0.png

Creating the database connection

Open your any kind of text editor(notepadd++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'db_crud'

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Cannot connect to the database"

    )

    ;
  5. }
  6. ?>

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 your text editor, then save it as shown below.
index.php.
  1. <!

    DOCTYPE html>
  2. <

    html lang=

    "en"

    >
  3. <

    head>
  4. <

    meta charset=

    "UTF-8"

    name=

    "viewport"

    content=

    "width=device-width, initial-scale=1"

    />
  5. <

    link

    rel=

    "stylesheet"

    type=

    "text/css"

    href=

    "css/bootstrap.css"

    />
  6. </

    head>
  7. <

    body>
  8. <

    nav class

    =

    "navbar navbar-default"

    >
  9. <

    div class

    =

    "container-fluid"

    >
  10. <

    a class

    =

    "navbar-brand"

    href=

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a>
  11. </

    div>
  12. </

    nav>
  13. <

    div class

    =

    "col-md-3"

    ></

    div>
  14. <

    div class

    =

    "col-md-6 well"

    >
  15. <

    h3 class

    =

    "text-primary"

    >

    PHP -

    Simple CRUD With Ajax/

    MySQLi</

    h3>
  16. <

    hr style=

    "border-top:1px dotted #ccc;"

    />
  17. <

    form method=

    "POST"

    >
  18. <

    div class

    =

    "form-inline"

    >
  19. <

    label>

    Firstname</

    label>
  20. <

    input type=

    "text"

    id=

    "firstname"

    class

    =

    "form-control"

    />
  21. <

    label>

    Lastname</

    label>
  22. <

    input type=

    "text"

    id=

    "lastname"

    class

    =

    "form-control"

    />
  23. </

    div>
  24. <

    br />
  25. <

    div class

    =

    "form-inline"

    >
  26. <

    label>

    Address</

    label>
  27. <

    input type=

    "text"

    id=

    "address"

    class

    =

    "form-control"

    />
  28. </

    div>
  29. <

    br />
  30. <

    center><

    button type=

    "button"

    id=

    "save"

    class

    =

    "btn btn-primary"

    ><

    span class

    =

    "glyphicon glyphicon-save"

    ></

    span>

    Save</

    button><

    button type=

    "button"

    id=

    "update"

    class

    =

    "btn btn-warning"

    ><

    span class

    =

    "glyphicon glyphicon-edit"

    ></

    span>

    Update</

    button></

    center>
  31. </

    form>
  32. <

    br />
  33. <

    table class

    =

    "table table-bordered"

    >
  34. <

    thead>
  35. <

    tr>
  36. <

    th>

    Name</

    th>
  37. <

    th>

    Address</

    th>
  38. <

    th>

    Action</

    th>
  39. </

    tr>
  40. </

    thead>
  41. <

    tbody id=

    "data"

    ></

    tbody>
  42. </

    table>
  43. </

    div>
  44. </

    body>
  45. <

    script src=

    "js/jquery-3.2.1.min.js"

    >

    </script>
  46. <

    script src=

    "js/script.js"

    >

    </script>
  47. </

    html>

Creating The Queries

This code contains the main function of the application. This code contains a different functionalities that needed to make this application work. This include the Create, Read, Update, Delete functionalities that will be called via the ajax method. To do that copy and write these block of codes inside your text editor, then save it as shown below.

save_query.php
This code will save the data input to the database server. This will process all the data then will be manage by the ajax request.
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  4. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  5. $address

    =

    $_POST

    [

    'address'

    ]

    ;

  6. $conn

    ->

    query

    (

    "INSERT INTO `member` VALUES('', '$firstname

    ', '$lastname

    ', '$address

    ')"

    )

    ;
  7. ?>

data_query.php
This code will display the data from the database. This will render the data from the database via ajax request.
  1. <?php
  2. require_once

    'conn.php'

    ;
  3. if

    (

    ISSET

    (

    $_POST

    [

    'res'

    ]

    )

    )

    {
  4. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `member`"

    )

    ;
  5. while

    (

    $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  6. echo
  7. "
  8. <tr>
  9. <td>"

    .

    $fetch

    [

    'firstname'

    ]

    .

    " "

    .

    $fetch

    [

    'lastname'

    ]

    .

    "</td>
  10. <td>"

    .

    $fetch

    [

    'address'

    ]

    .

    "</td>
  11. <td><center><button class='btn btn-warning edit' name='"

    .

    $fetch

    [

    'mem_id'

    ]

    .

    "'><span class='glyphicon glyphicon-edit'></span> Edit</button> | <button class='btn btn-danger delete' name='"

    .

    $fetch

    [

    'mem_id'

    ]

    .

    "'><span class='glyphicon glyphicon-trash'></span> Delete</button></center></td>
  12. </tr>
  13. "

    ;

  14. }
  15. }

  16. ?>

delete_query,php
This code will delete the data from the database. This will delete the record from database when the target button is clicked.
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. $id

    =

    $_POST

    [

    'id'

    ]

    ;

  4. $conn

    ->

    query

    (

    "DELETE FROM `member` WHERE `mem_id` = '$id

    '"

    )

    ;
  5. ?>

edit.php
This code will display the data from the database base on the click row data. This will populate the inputs based on the row that has been clicked.
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'id'

    ]

    )

    )

    {
  4. $id

    =

    $_POST

    [

    'id'

    ]

    ;

  5. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `member` WHERE `mem_id` ='$id

    '"

    )

    ;
  6. $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    ;

  7. $array

    =

    array

    (
  8. 'mem_id'

    =>

    $fetch

    [

    'mem_id'

    ]

    ,
  9. 'firstname'

    =>

    $fetch

    [

    'firstname'

    ]

    ,
  10. 'lastname'

    =>

    $fetch

    [

    'lastname'

    ]

    ,
  11. 'address'

    =>

    $fetch

    [

    'address'

    ]
  12. )

    ;

  13. echo

    json_encode

    (

    $array

    )

    ;
  14. }
  15. ?>

update_query.php
This code will update the data from the database base on the click row data. This will checked and update the data from the database based on the selected row data.
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'id'

    ]

    )

    )

    {
  4. $id

    =

    $_POST

    [

    'id'

    ]

    ;
  5. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  6. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  7. $address

    =

    $_POST

    [

    'address'

    ]

    ;

  8. $conn

    ->

    query

    (

    "UPDATE `member` set `firstname` = '$firstname

    ', `lastname` = '$lastname

    ', `address` = '$address

    ' WHERE `mem_id` = '$id

    '"

    )

    ;
  9. }

  10. ?>

Creating The Ajax Funcition

This is where the code that uses ajax script. This code contains a different functionalities that manage the php queries to be able to send some information to the database. To do that just simply copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. var

    mem_id;

  3. DisplayData(

    )

    ;

  4. $(

    '#update'

    )

    .hide

    (

    )

    ;

  5. $(

    '#save'

    )

    .on

    (

    'click'

    ,

    function

    (

    )

    {
  6. if

    (

    $(

    '#firstname'

    )

    .val

    (

    )

    ==

    ""

    ||

    $(

    '#lastname'

    )

    .val

    (

    )

    ==

    ""

    ||

    $(

    '#address'

    )

    .val

    (

    )

    ==

    ""

    )

    {
  7. alert(

    "Hello World"

    )

    ;
  8. }

    else

    {
  9. var

    firstname =

    $(

    '#firstname'

    )

    .val

    (

    )

    ;
  10. var

    lastname =

    $(

    '#lastname'

    )

    .val

    (

    )

    ;
  11. var

    address =

    $(

    '#address'

    )

    .val

    (

    )

    ;

  12. $.ajax

    (

    {
  13. url:

    'save_query.php'

    ,
  14. type:

    'POST'

    ,
  15. data:

    {
  16. firstname:

    firstname,
  17. lastname:

    lastname,
  18. address:

    address
  19. }

    ,
  20. success:

    function

    (

    data)

    {
  21. $(

    '#firstname'

    )

    .val

    (

    ''

    )

    ;
  22. $(

    '#lastname'

    )

    .val

    (

    ''

    )

    ;
  23. $(

    '#address'

    )

    .val

    (

    ''

    )

    ;
  24. DisplayData(

    )

    ;
  25. }
  26. }

    )

    ;
  27. }

  28. }

    )

    ;

  29. function

    DisplayData(

    )

    {
  30. $.ajax

    (

    {
  31. url:

    'data_query.php'

    ,
  32. type:

    'POST'

    ,
  33. data:

    {
  34. res:

    1
  35. }

    ,
  36. success:

    function

    (

    response)

    {
  37. $(

    '#data'

    )

    .html

    (

    response)

    ;
  38. }
  39. }

    )
  40. }

  41. $(

    document)

    .on

    (

    'click'

    ,

    '.delete'

    ,

    function

    (

    )

    {
  42. var

    id =

    $(

    this

    )

    .attr

    (

    'name'

    )

    ;

  43. $.ajax

    (

    {
  44. url:

    'delete_query.php'

    ,
  45. type:

    'POST'

    ,
  46. data:

    {
  47. id:

    id
  48. }

    ,
  49. success:

    function

    (

    data)

    {
  50. DisplayData(

    )

    ;
  51. $(

    '#update'

    )

    .hide

    (

    )

    ;
  52. $(

    '#save'

    )

    .show

    (

    )

    ;
  53. $(

    '#firstname'

    )

    .val

    (

    ''

    )

    ;
  54. $(

    '#lastname'

    )

    .val

    (

    ''

    )

    ;
  55. $(

    '#address'

    )

    .val

    (

    ''

    )

    ;
  56. }
  57. }

    )

    ;
  58. }

    )

  59. $(

    document)

    .on

    (

    'click'

    ,

    '.edit'

    ,

    function

    (

    )

    {
  60. var

    id =

    $(

    this

    )

    .attr

    (

    'name'

    )

    ;

  61. $.ajax

    (

    {
  62. url:

    'edit.php'

    ,
  63. type:

    'POST'

    ,
  64. data:

    {
  65. id:

    id
  66. }

    ,
  67. success:

    function

    (

    response)

    {
  68. var

    getArray =

    jQuery.parseJSON

    (

    response)

    ;

  69. mem_id =

    getArray.mem_id

    ;

  70. $(

    '#firstname'

    )

    .val

    (

    getArray.firstname

    )

    ;
  71. $(

    '#lastname'

    )

    .val

    (

    getArray.lastname

    )

    ;
  72. $(

    '#address'

    )

    .val

    (

    getArray.address

    )

    ;

  73. $(

    '#update'

    )

    .show

    (

    )

    ;
  74. $(

    '#save'

    )

    .hide

    (

    )

    ;
  75. }
  76. }

    )
  77. }

    )

    ;

  78. $(

    '#update'

    )

    .on

    (

    'click'

    ,

    function

    (

    )

    {
  79. var

    firstname =

    $(

    '#firstname'

    )

    .val

    (

    )

    ;
  80. var

    lastname =

    $(

    '#lastname'

    )

    .val

    (

    )

    ;
  81. var

    address =

    $(

    '#address'

    )

    .val

    (

    )

    ;


  82. $.ajax

    (

    {
  83. url:

    'update_query.php'

    ,
  84. type:

    'POST'

    ,
  85. data:

    {
  86. id:

    mem_id,
  87. firstname:

    firstname,
  88. lastname:

    lastname,
  89. address:

    address
  90. }

    ,
  91. success:

    function

    (

    )

    {
  92. DisplayData(

    )

    ;
  93. $(

    '#firstname'

    )

    .val

    (

    ''

    )

    ;
  94. $(

    '#lastname'

    )

    .val

    (

    ''

    )

    ;
  95. $(

    '#address'

    )

    .val

    (

    ''

    )

    ;

  96. alert(

    "Successfully Updated!"

    )

    ;

  97. $(

    '#update'

    )

    .hide

    (

    )

    ;
  98. $(

    '#save'

    )

    .show

    (

    )

    ;

  99. mem_id =

    ""

    ;
  100. }
  101. }

    )

    ;
  102. }

    )

    ;
  103. }

    )

    ;

There you have it we successfully created a Simple CRUD With Ajax/MySQLi using PHP. 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!!!


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

452,292

323,341

323,350

Top