PHP - Update Data In SQLite3

Submitted by: 
Language: 
Visitors have accessed this post 110 times.

In this tutorial we will create a Update Data In SQLite3 using PHP. This code will update a data in SQLite database when user click the edit button in the table. The code use href or Hypertext Referencing to direct the user to a page that has been binded by an id in order to update a data using UPDATE query and applying condition in the WHERE clause from where the value is the binded id. This a free program, you can apply this to your system as your own.

We will be using SQLite that 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.

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 SQLite3('db/db_member') or die("Unable to open database!");
  3. $query="CREATE TABLE IF NOT EXISTS `member`(mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, firstname TEXT, lastname TEXT, address TEXT)";
  4.  
  5. $conn->exec($query);
  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 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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Update Data In SQLite3</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form method="POST" action="insert.php">
  20. <div class="form-group">
  21. <label>Firstname</label>
  22. <input type="text" class="form-control" name="firstname" required="required"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Lastname</label>
  26. <input type="text" class="form-control" name="lastname" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Address</label>
  30. <input type="text" class="form-control" name="address" required="required"/>
  31. </div>
  32. <center><button class="btn btn-primary" name="insert" >Insert</button></center>
  33. </form>
  34. </div>
  35. <div class="col-md-8">
  36. <table class="table table-bordered">
  37. <thead class="alert-info">
  38. <tr >
  39. <th>Firstname</th>
  40. <th>Lastname</th>
  41. <th>Address</th>
  42. <th>Action</th>
  43. </tr>
  44. </thead>
  45. <tbody>
  46. <?php
  47. require'conn.php';
  48.  
  49. $query=$conn->query("SELECT * FROM `member`") or die("Failed to fetch row!");
  50. while($fetch=$query->fetchArray()){
  51. ?>
  52. <tr>
  53. <td><?php echo $fetch['firstname']?></td>
  54. <td><?php echo $fetch['lastname']?></td>
  55. <td><?php echo $fetch['address']?></td>
  56. <td><button type="button" class="btn btn-warning" data-toggle="modal" data-target="#form_modal<?php echo $fetch['mem_id']?>"><span class="glyphicon glyphicon-edit"></span> Edit</button></td>
  57. </tr>
  58. <div class="modal fade" id="form_modal<?php echo $fetch['mem_id']?>" aria-hidden="true">
  59. <div class="modal-dialog">
  60. <div class="modal-content">
  61. <form method="POST" action="update.php">
  62. <div class="modal-header">
  63. <h3 class="modal-title">Update User</h3>
  64. </div>
  65. <div class="modal-body">
  66. <div class="col-md-2"></div>
  67. <div class="col-md-8">
  68. <div class="form-group">
  69. <label>Firstname</label>
  70. <input type="hidden" name="mem_id" value="<?php echo $fetch['mem_id']?>"/>
  71. <input type="text" class="form-control" name="firstname" value="<?php echo $fetch['firstname']?>" required="required"/>
  72. </div>
  73. <div class="form-group">
  74. <label>Lastname</label>
  75. <input type="text" class="form-control" name="lastname" value="<?php echo $fetch['lastname']?>" required="required"/>
  76. </div>
  77. <div class="form-group">
  78. <label>Address</label>
  79. <input type="text" class="form-control" name="address" value="<?php echo $fetch['address']?>" required="required"/>
  80. </div>
  81. </div>
  82. </div>
  83. <div style="clear:both;"></div>
  84. <div class="modal-footer">
  85. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-trash"></span> Close</button>
  86. <button class="btn btn-warning" name="update"><span class="glyphicon glyphicon-save"></span> Update</button>
  87. </div>
  88. </form>
  89. </div>
  90. </div>
  91. </div>
  92. <?php
  93. }
  94. ?>
  95. </tbody>
  96. </table>
  97. </div>
  98. </div>
  99. <script src="js/jquery-3.2.1.min.js"></script>
  100. <script src="js/bootstrap.js"></script>
  101. </body>
  102. </html>

Creating the SQLite Query

This code contains the SQLite query of the application. This code will store the data inputs to SQLite database. To do this just copy and write these block of codes as shown below inside the text editor, then save it as insert.php.

  1. <?php
  2. require_once'conn.php';
  3. if(ISSET($_POST['insert'])){
  4. $firstname=$_POST['firstname'];
  5. $lastname=$_POST['lastname'];
  6. $address=$_POST['address'];
  7.  
  8.  
  9. $query="INSERT INTO `member` (firstname, lastname, address) VALUES('$firstname', '$lastname', '$address')";
  10.  
  11. $conn->exec($query);
  12.  
  13. header("location:index.php");
  14. }
  15. ?>

Creating the Main Function

This code contains the main function of the application. This code will update a data in the SQLite database. To do this just copy and write these block of codes as shown below inside the text editor, then save it as update.php.

  1. <?php
  2. require_once'conn.php';
  3.  
  4. if(ISSET($_POST['update'])){
  5. $mem_id=$_POST['mem_id'];
  6. $firstname=$_POST['firstname'];
  7. $lastname=$_POST['lastname'];
  8. $address=$_POST['address'];
  9.  
  10.  
  11. $query="UPDATE `member` SET `firstname`='$firstname', `lastname`='$lastname', `address`='$address' WHERE `mem_id`='$mem_id'";
  12.  
  13. $conn->exec($query);
  14.  
  15. header("location:index.php");
  16. }
  17. ?>

There you have it we successfully created a Update Data In SQLite3 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!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.