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

PHP - Complete PDO CRUD

rockyouman

Technical SEO Ninja
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
130
Likes
167
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Complete PDO CRUD using PDO. This code has several functionalities that can manipulate a data through database server using PDO query. The code use a PDO query to (Create, Read, Update, Delete) a certain data to a database server with a high data protection in order to avoid data injection tools.

We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much easier.

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/.

Creating Database

Open your database web server then create a database name in it db_pdo_crud, after that click Import then locate the database file inside the folder of the application then click ok.
2019-09-22_00_33_59-localhost_127.0.0.1_db_pdo_crud_phpmyadmin_4.8.3.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. $db_username

    =

    'root'

    ;
  3. $db_password

    =

    ''

    ;
  4. $conn

    =

    new

    PDO(

    'mysql:host=localhost;dbname=db_pdo_crud'

    ,

    $db_username

    ,

    $db_password

    )

    ;
  5. if

    (

    !

    $conn

    )

    {
  6. die

    (

    "Fatal Error: Connection Failed!"

    )

    ;
  7. }
  8. ?>

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 as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a href="https://sourcecodester.com" class="navbar-brand">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 - Complete PDO CRUD</h3>
  16. <hr style="border-top:1px dotted #ccc;" />
  17. <div class="col-md-3">
  18. <form method="POST" action="add.php">
  19. <div class="form-group">
  20. <label>Firstname</label>
  21. <input class="form-control" type="text" name="firstname"/>
  22. </div>
  23. <div class="form-group">
  24. <label>Lastname</label>
  25. <input class="form-control" type="text" name="lastname"/>
  26. </div>
  27. <div class="form-group">
  28. <label>Address</label>
  29. <input class="form-control" type="text" name="address"/>
  30. </div>
  31. <div class="form-group">
  32. <button class="btn btn-primary form-control" type="submit" name="save">Save</button>
  33. </div>
  34. </form>
  35. </div>
  36. <div class="col-md-9">
  37. <table class="table table-bordered">
  38. <thead class="alert-info">
  39. <tr>
  40. <th>Firstname</th>
  41. <th>Lastname</th>
  42. <th>Address</th>
  43. <th>Action</th>
  44. </tr>
  45. </thead>
  46. <tbody>
  47. <?php
  48. require

    'conn.php'

    ;
  49. $sql

    =

    $conn

    ->

    prepare

    (

    "SELECT * FROM `user`"

    )

    ;
  50. $sql

    ->

    execute

    (

    )

    ;
  51. while

    (

    $fetch

    =

    $sql

    ->

    fetch

    (

    )

    )

    {
  52. ?>
  53. <tr>
  54. <td><?php

    echo

    $fetch

    [

    'firstname'

    ]

    ?>

    </td>
  55. <td><?php

    echo

    $fetch

    [

    'lastname'

    ]

    ?>

    </td>
  56. <td><?php

    echo

    $fetch

    [

    'address'

    ]

    ?>

    </td>
  57. <td><button class="btn btn-warning btn-sm" data-toggle="modal" data-target="#update<?php

    echo

    $fetch

    [

    'user_id'

    ]

    ?>

    ">Edit</button> | <a class="btn btn-danger btn-sm" href="delete.php?id=<?php

    echo

    $fetch

    [

    'user_id'

    ]

    ?>

    ">Delete</a></td>
  58. </tr>

  59. <div class="modal fade" id="update<?php

    echo

    $fetch

    [

    'user_id'

    ]

    ?>

    " aria-hidden="true">
  60. <div class="modal-dialog">
  61. <div class="modal-content">
  62. <form method="POST" action="update.php">
  63. <div class="modal-header">
  64. <h3 class="modal-title">Update User</h3>
  65. </div>
  66. <div class="modal-body">
  67. <div class="col-md-2"></div>
  68. <div class="col-md-8">
  69. <div class="form-group">
  70. <label>Firstname</label>
  71. <input class="form-control" type="text" value="<?php

    echo

    $fetch

    [

    'firstname'

    ]

    ?>

    " name="firstname"/>
  72. <input type="hidden" value="<?php

    echo

    $fetch

    [

    'user_id'

    ]

    ?>

    " name="user_id"/>
  73. </div>
  74. <div class="form-group">
  75. <label>Lastname</label>
  76. <input class="form-control" type="text" value="<?php

    echo

    $fetch

    [

    'lastname'

    ]

    ?>

    " name="lastname"/>
  77. </div>
  78. <div class="form-group">
  79. <label>Address</label>
  80. <input class="form-control" type="text" value="<?php

    echo

    $fetch

    [

    'address'

    ]

    ?>

    " name="address"/>
  81. </div>
  82. <div class="form-group">
  83. <button class="btn btn-warning form-control" type="submit" name="update">Update</button>
  84. </div>
  85. </div>
  86. </div>
  87. <br style="clear:both;"/>
  88. <div class="modal-footer">
  89. <button class="btn btn-danger" data-dismiss="modal">Close</button>
  90. </div>
  91. </form>
  92. </div>
  93. </div>
  94. </div>

  95. <?php
  96. }
  97. ?>
  98. </tbody>
  99. </table>
  100. </div>
  101. </div>
  102. <script src="js/jquery-3.2.1.min.js"></script>
  103. <script src="js/bootstrap.js"></script>
  104. </body>
  105. </html>

Creating the Main Function

This code contains the main function of the application. This code can manipulate a data when the button is clicked. To do that write these block of codes inside the text editor and save it as shown below.
add.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  4. try{
  5. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  6. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  7. $address

    =

    $_POST

    [

    'address'

    ]

    ;
  8. $conn

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;
  9. $sql

    =

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

    ', '$lastname

    ', '$address

    ')"

    ;
  10. $conn

    ->

    exec

    (

    $sql

    )

    ;
  11. }

    catch(

    PDOException $e

    )

    {
  12. echo

    $e

    ->

    getMessage

    (

    )

    ;
  13. }

  14. $conn

    =

    null

    ;
  15. header

    (

    'location:index.php'

    )

    ;
  16. }
  17. ?>

update.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'update'

    ]

    )

    )

    {
  4. try{
  5. $user_id

    =

    $_POST

    [

    'user_id'

    ]

    ;
  6. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  7. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  8. $address

    =

    $_POST

    [

    'address'

    ]

    ;
  9. $conn

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;
  10. $sql

    =

    "UPDATE `user`SET `firstname` = '$firstname

    ', `lastname` = '$lastname

    ', `address` = '$address

    ' WHERE `user_id` = '$user_id

    '"

    ;
  11. $conn

    ->

    exec

    (

    $sql

    )

    ;
  12. }

    catch(

    PDOException $e

    )

    {
  13. echo

    $e

    ->

    getMessage

    (

    )

    ;
  14. }

  15. $conn

    =

    null

    ;
  16. header

    (

    'location:index.php'

    )

    ;
  17. }
  18. ?>

delete.php
  1. <?php
  2. if

    (

    ISSET

    (

    $_GET

    [

    'id'

    ]

    )

    )

    {
  3. require_once

    'conn.php'

    ;
  4. $id

    =

    $_GET

    [

    'id'

    ]

    ;
  5. $sql

    =

    $conn

    ->

    prepare

    (

    "DELETE from `user` WHERE `user_id`='$id

    '"

    )

    ;
  6. $sql

    ->

    execute

    (

    )

    ;
  7. header

    (

    'location:index.php'

    )

    ;
  8. }
  9. ?>

There you have it we successfully created a Complete PDO CRUD using PDO. 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 the hidden content.
 

452,292

324,736

324,744

Top