Skip to main content

PHP - Insert Data to SQLite Using PDO

In this tutorial we will create a Insert Data to SQLite Using PDO. PHP is a server-side scripting language designed primarily for web development. SQLite provides an interface for accessing the database. It includes class interfaces to the SQL commands. And also it allows you to create SQL functions and aggregate using PHP. 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

And this is the link for the jquery that i used in this tutorial

Lastly, this is the link for the bootstrap that i used for the layout design

Installing SQLite Browser
We will now then install the SQLite data viewer, here's the link for the DB Browser for SQLite

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.
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_member.sqlite3');
  4. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  6. $query = "CREATE TABLE IF NOT EXISTS member (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, firstname TEXT, lastname TEXT, address TEXT)";
  8. $conn->exec($query);
  10. ?>

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="">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 - Insert Data to 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 Member</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>Address</th>
  25. </tr>
  26. </thead>
  27. <tbody style="background-color:#fff;">
  28. <?php
  29. require 'conn.php';
  31. $query = $conn->prepare("SELECT * FROM `member` ORDER BY `lastname` ASC");
  32. $query->execute();
  33. while($fetch = $query->fetch()){
  34. ?>
  35. <tr>
  36. <td><?php echo $fetch['firstname']?></td>
  37. <td><?php echo $fetch['lastname']?></td>
  38. <td><?php echo $fetch['address']?></td>
  39. </tr>
  40. <?php
  41. }
  43. $conn = null;
  44. ?>
  45. </tbody>
  46. </table>
  47. </div>
  48. <div class="modal fade" id="form_modal" aria-hidden="true">
  49. <div class="modal-dialog">
  50. <div class="modal-content">
  51. <form method="POST" action="save_member.php">
  52. <div class="modal-header">
  53. <div class="modal-title">
  54. <h3>Add Member</h3>
  55. </div>
  56. </div>
  57. <div class="modal-body">
  58. <div class="col-md-2"></div>
  59. <div class="col-md-8">
  60. <div class="form-group">
  61. <label>Firstname</label>
  62. <input type="text" name="firstname" class="form-control" required="required"/>
  63. </div>
  64. <div class="form-group">
  65. <label>Lastname</label>
  66. <input type="text" name="lastname" class="form-control" required="required"/>
  67. </div>
  68. <div class="form-group">
  69. <label>Address</label>
  70. <input type="text" name="address" class="form-control" required="required"/>
  71. </div>
  72. </div>
  73. </div>
  74. <div style="clear:both;"></div>
  75. <div class="modal-footer">
  76. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  77. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  78. </div>
  79. </form>
  80. </div>
  81. </div>
  82. </div>
  83. <script src="js/jquery-3.2.1.min.js"></script>
  84. <script src="js/bootstrap.js"></script>
  85. </body>
  86. </html>

Creating the Main Function

This code contains the main function of the application. This code will store the data inputs to the SQLite database using PDO queries. To do this just copy and write these block of codes as shown below inside the text editor and save it as save_member.php.

  1. <?php
  2. require_once 'conn.php';
  5. if(ISSET($_POST['save'])){
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. $address = $_POST['address'];
  10. $query = "INSERT INTO member (firstname, lastname, address) VALUES (:firstname, :lastname, :address)";
  12. $stmt = $conn->prepare($query);
  14. $stmt->bindParam(':firstname', $firstname);
  15. $stmt->bindParam(':lastname', $lastname);
  16. $stmt->bindParam(':address', $address);
  18. $stmt->execute();
  20. header('location: index.php');
  22. $conn = null;
  23. }
  24. ?>

There you have it we successfully created a Insert Data to 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!!!

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 for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


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.

Submitted byoleteacheron Thu, 09/27/2018 - 11:00

Thanks for showing PDO / SQLite tutorial. We have been learning PDO as provides better protection.

Looking forward to possible future tuts on editing and deleting with PDO / SQLite.

Thank you again,


Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.