PHP - Delete Multiple Row To SQLite Using PDO

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

In this tutorial we will create a Delete Multiple Row 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 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, student_no INTEGER, firstname TEXT, lastname 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="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 - Delete Multiple Row To SQLite Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-primary" type="button" 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. <form method="POST" action="delete.php">
  21. <thead class="alert-info">
  22. <tr>
  23. <th><input type="checkbox" onclick="selectedCheckbox(this)"> Select All</th>
  24. <th>Student No</th>
  25. <th>Firstname</th>
  26. <th>Lastname</th>
  27. <th>Address</th>
  28. </tr>
  29. </thead>
  30. <tbody style="background-color:#fff;">
  31. <?php
  32. require 'conn.php';
  33. $query = $conn->prepare("SELECT * FROM `student` ORDER BY `lastname` ASC");
  34. $query->execute();
  35. $count_query = $conn->prepare("SELECT COUNT(*) as count FROM `student` ORDER BY `lastname` ASC");
  36. $count_query->execute();
  37. $row = $count_query->fetch();
  38. $count = $row['count'];
  39. while($fetch = $query->fetch()){
  40. ?>
  41. <tr>
  42. <?php
  43. if($count != 0){
  44. ?>
  45. <td><input type="checkbox" name="check[]" onclick="countCheckbox()" value="<?php echo $fetch['student_id']?>"></td>
  46. <?php
  47. }
  48. ?>
  49. <td><?php echo $fetch['student_no']?></td>
  50. <td><?php echo $fetch['firstname']?></td>
  51. <td><?php echo $fetch['lastname']?></td>
  52. <td><?php echo $fetch['address']?></td>
  53. </tr>
  54. <?php
  55. }
  56. ?>
  57. </tbody>
  58. <?php
  59. if($count != 0){
  60. ?>
  61. <tfoot>
  62. <tr>
  63. <td colspan="5"><button name="delete" class="btn btn-danger pull-right"><span id="count" class="badge">0</span> Delete</button></td>
  64. </tr>
  65. </tfoot>
  66. <?php
  67. }
  68. ?>
  69. </form>
  70. </table>
  71. </div>
  72. <div class="modal fade" id="form_modal" aria-hidden="true">
  73. <div class="modal-dialog">
  74. <div class="modal-content">
  75. <form method="POST" action="save_student.php">
  76. <div class="modal-header">
  77. <h3 class="modal-title">Add Student</h3>
  78. </div>
  79. <div class="modal-body">
  80. <div class="col-md-2"></div>
  81. <div class="col-md-8">
  82. <div class="form-group">
  83. <label>Student No</label>
  84. <input type="text" name="student_no" class="form-control" required="required"/>
  85. </div>
  86. <div class="form-group">
  87. <label>Firstname</label>
  88. <input type="text" name="firstname" class="form-control" required="required"/>
  89. </div>
  90. <div class="form-group">
  91. <label>Lastname</label>
  92. <input type="text" name="lastname" class="form-control" required="required"/>
  93. </div>
  94. <div class="form-group">
  95. <label>Address</label>
  96. <input type="text" name="address" class="form-control" required="required"/>
  97. </div>
  98. </div>
  99. </div>
  100. <div style="clear:both;"></div>
  101. <div class="modal-footer">
  102. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  103. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  104. </div>
  105. </form>
  106. </div>
  107. </div>
  108. </div>
  109. <script src="js/jquery-3.2.1.min.js"></script>
  110. <script src="js/bootstrap.js"></script>
  111. <script src="js/script.js"></script>
  112. </body>
  113. </html>

Creating the PHP Query

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

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

Creating the Script

This code contains the script of the application. This code will count the checkboxes that will be remove by the user. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.

  1. function selectedCheckbox(check){
  2. var checkboxes = document.getElementsByName('check[]');
  3. for(var i in checkboxes){
  4. checkboxes[i].checked = check.checked;
  5. }
  6.  
  7. var count = document.querySelectorAll('input[name="check[]"]:checked').length;
  8. document.getElementById('count').innerHTML = count;
  9. }
  10.  
  11. function countCheckbox(){
  12. var count = document.querySelectorAll('input[name="check[]"]:checked').length;
  13. document.getElementById('count').innerHTML = count;
  14. }

Creating the Main Function

This code contains the main function of the application. This code will delete the data from the database base on the checked item in the row. To do this just copy and write these block of codes as shown below inside the text editor, then save it as delete.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['delete'])){
  5. if(ISSET($_POST['check'])){
  6. $checked = $_POST['check'];
  7. for($i=0; $i < count($checked); $i++){
  8. $id = $checked[$i];
  9. $query = "DELETE FROM `student` WHERE `student_id` = :student_id";
  10. $stmt = $conn->prepare($query);
  11. $stmt->bindParam(':student_id', $id);
  12. $stmt->execute();
  13. }
  14.  
  15. $conn = null;
  16. header('location:index.php');
  17. }else{
  18. echo "<script>alert('Please select an item first!')</script>";
  19. echo "<script>window.location='index.php'</script>";
  20. }
  21. }
  22. ?>

There you have it we successfully created a Delete Multiple Row 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!!!


Comments

Nice Project I really like it

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.