PHP - Simple CRUD With SQLite Using PDO

In this tutorial we will create a Insert Data to SQLite Using PDO. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. SQLite is an in-process library that implements a self-contained and a transactional SQL database engine. SQLite generally runs faster the more memory you give it. That's why most of the developer use SQLite as a database engine for their application. So Let's do the coding...

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 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/. Installing SQLite Browser We will now then install the SQLite data viewer, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/.

Setting Up SQLite

First, we are going to enable SQLite 3 in our PHP. 1. Open localhost server folder XAMPP, etc and locate php.ini. 2. Open php.ini and enable sqlite3 by removing the semicolon in the line. tut1 3. Save changes and Restart Server.

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 = new PDO('sqlite:db/db_student.sqlite3');
  3.  
  4. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  5.  
  6. $query = "CREATE TABLE IF NOT EXISTS student (student_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, firstname TEXT, lastname TEXT, gender TEXT, address TEXT)";
  7.  
  8. $conn->exec($query);
  9. ?>

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. <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="css/bootstrap.css">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 SQLite Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Student</button>
  18. <br /><br/ >
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Firstname</th>
  23. <th>Lastname</th>
  24. <th>Gender</th>
  25. <th>Address</th>
  26. <th>Action</th>
  27. </tr>
  28. </thead>
  29. <tbody>
  30. <?php
  31. require 'conn.php';
  32. $query = $conn->prepare("SELECT * FROM `student`");
  33. $query->execute();
  34. while($fetch = $query->fetch()){
  35. ?>
  36. <tr>
  37. <td><?php echo $fetch['firstname']?></td>
  38. <td><?php echo $fetch['lastname']?></td>
  39. <td><?php echo $fetch['gender']?></td>
  40. <td><?php echo $fetch['address']?></td>
  41. <td><button class="btn btn-warning" type="button" data-toggle="modal" data-target="#update_student<?php echo $fetch['student_id']?>"><span class="glyphicon glyphicon-edit"></span> Edit</button> <a href="delete.php?id=<?php echo $fetch['student_id']?>" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</a></td>
  42. </tr>
  43.  
  44. <div class="modal fade" id="update_student<?php echo $fetch['student_id']?>">
  45. <div class="modal-dialog">
  46. <div class="modal-content">
  47. <form action="update_student.php" method="POST">
  48. <div class="modal-header">
  49. <h3 class="modal-title">Update Student</h3>
  50. </div>
  51. <div class="modal-body">
  52. <div class="col-md-2"></div>
  53. <div class="col-md-8">
  54. <div class="form-group">
  55. <label>Firstname</label>
  56. <input type="text" class="form-control" value="<?php echo $fetch['firstname']?>" name="firstname"/>
  57. <input type="hidden" class="form-control" value="<?php echo $fetch['student_id']?>" name="student_id"/>
  58. </div>
  59. <div class="form-group">
  60. <label>Lastname</label>
  61. <input type="text" class="form-control" value="<?php echo $fetch['lastname']?>" name="lastname"/>
  62. </div>
  63. <div class="form-group">
  64. <label>Gender</label>
  65. <div class="radio">
  66. <label><input type="radio" name="gender" value="Male" required="required"/>Male</label>
  67. <label><input type="radio" name="gender" value="Female"/>Female</label>
  68. </div>
  69. </div>
  70. <div class="form-group">
  71. <label>Address</label>
  72. <input type="text" class="form-control" value="<?php echo $fetch['address']?>" name="address"/>
  73. </div>
  74. </div>
  75. </div>
  76. <div style="clear:both;"></div>
  77. <div class="modal-footer">
  78. <button class="btn btn-warning" name="update"><span class="glyphicon glyphicon-update"></span> Update</button>
  79. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  80. </div>
  81. </form>
  82. </div>
  83. </div>
  84. </div>
  85.  
  86. <?php
  87. }
  88. ?>
  89. </tbody>
  90. </table>
  91. </div>
  92. <div class="modal fade" id="form_modal" aria-hidden="true">
  93. <div class="modal-dialog">
  94. <div class="modal-content">
  95. <form method="POST" action="save_student.php">
  96. <div class="modal-header">
  97. <h3 class="modal-title">Add Student</h3>
  98. </div>
  99. <div class="modal-body">
  100. <div class="col-md-2"></div>
  101. <div class="col-md-8">
  102. <div class="form-group">
  103. <label>Firstname</label>
  104. <input type="text" class="form-control" name="firstname"/>
  105. </div>
  106. <div class="form-group">
  107. <label>Lastname</label>
  108. <input type="text" class="form-control" name="lastname"/>
  109. </div>
  110. <div class="form-group">
  111. <label>Gender</label>
  112. <div class="radio">
  113. <label><input type="radio" name="gender" value="Male" required="required"/>Male</label>
  114. <label><input type="radio" name="gender" value="Female"/>Female</label>
  115. </div>
  116. </div>
  117. <div class="form-group">
  118. <label>Address</label>
  119. <input type="text" class="form-control" name="address"/>
  120. </div>
  121. </div>
  122. </div>
  123. <div style="clear:both;"></div>
  124. <div class="modal-footer">
  125. <button class="btn btn-primary" name="save">Save</button>
  126. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  127. </div>
  128. </form>
  129. </div>
  130. </div>
  131. </div>
  132. <script src="js/jquery-3.2.1.min.js"></script>
  133. <script src="js/bootstrap.js"></script>
  134. </body>
  135. </html>

Creating the Main Function

This code contains the main function of the application. This code is consist of different functionalities: it can add the data inputs, can update data, and also it can delete the data in the database. To do this just copy and write these block of codes inside the text editor and save it as shown below. save_student.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $gender = $_POST['gender'];
  8. $address = $_POST['address'];
  9.  
  10. $query = "INSERT INTO `student` (firstname, lastname, gender, address) VALUES(:firstname, :lastname, :gender, :address)";
  11.  
  12. $stmt = $conn->prepare($query);
  13. $stmt->bindParam(':firstname', $firstname);
  14. $stmt->bindParam(':lastname', $lastname);
  15. $stmt->bindParam(':gender', $gender);
  16. $stmt->bindParam(':address', $address);
  17.  
  18. $stmt->execute();
  19. $conn = null;
  20.  
  21. header('location: index.php');
  22.  
  23. }
  24. ?>
update_student.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['update'])){
  5. $student_id = $_POST['student_id'];
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. $gender = $_POST['gender'];
  9. $address = $_POST['address'];
  10.  
  11. $query = "UPDATE `student` SET `firstname` = :firstname, `lastname` = :lastname, `gender` = :gender, `address` = :address WHERE `student_id` = :student_id";
  12.  
  13. $stmt = $conn->prepare($query);
  14. $stmt->bindParam(':firstname', $firstname);
  15. $stmt->bindParam(':lastname', $lastname);
  16. $stmt->bindParam(':gender', $gender);
  17. $stmt->bindParam(':address', $address);
  18. $stmt->bindParam(':student_id', $student_id);
  19.  
  20. $stmt->execute();
  21. $conn = null;
  22.  
  23. header('location: index.php');
  24.  
  25. }
  26. ?>
delete.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_REQUEST['id'])){
  5. $query = "DELETE FROM `student` WHERE student_id = '$_REQUEST[id]'";
  6. $stmt = $conn->prepare($query);
  7. $stmt->execute();
  8. $conn = null;
  9.  
  10. header('location: index.php');
  11. }
  12.  
  13. ?>
There you have it we successfully created a Simple CRUD With SQLite 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!!!

Comments

Submitted byoleteacheron Fri, 09/28/2018 - 20:12

Thank you for the tutorial. Note: the downloaded files (in zip) do not seem to be correct.

I mistakenly uploaded an incorrect file for these tutorial. I already re upload the file you can now re download again.
Submitted byoleteacheron Sat, 09/29/2018 - 03:20

Every thing looks correct in the delete.php file according to official sqlite docs but when deleting a student, data is not actually removed from the db_student.sqlite3 Is there anything else that can be added to delete.php to insure all associated data remoed from db_student.sqlite3 ? Thank you again for tut!

Add new comment