PHP - Simple Search Filter To SQLite Using PDO

In this tutorial we will create a Simple Search Filter 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_product.sqlite3');
  3.        
  4.         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  5.        
  6.         $query = "CREATE TABLE IF NOT EXISTS product (product_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, product_name TEXT, product_price TEXT, product_image 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 - Simple Search Filter To SQLite Using PDO</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Product</button>
  18.                 <form method="POST" class="form-inline pull-right" action="search.php">
  19.                         <label>Search:</label>
  20.                         <input type="text" name="keyword" class="form-control" placeholder="Enter Product Name..." required="required"/>
  21.                         <button class="btn btn-success" name="search"><span class="glyphicon glyphicon-search"></span> Search</button>
  22.                 </form>
  23.                 <br /><br />
  24.                 <table class="table table-bordered">
  25.                         <thead class="alert-info">
  26.                                 <tr>
  27.                                         <th>Product Name</th>
  28.                                         <th>Product Price</th>
  29.                                         <th>Image</th>
  30.                                 </tr>
  31.                         </thead>
  32.                         <tbody style="background-color:#fff;">
  33.                                 <?php
  34.                                         require 'conn.php';
  35.                                         $query = $conn->prepare("SELECT * FROM `product` ORDER BY `product_name` ASC");
  36.                                         $query->execute();
  37.                                         while($fetch = $query->fetch()){
  38.                                 ?>
  39.                                 <tr>
  40.                                         <td><?php echo $fetch['product_name']?></td>
  41.                                         <td><?php echo number_format($fetch['product_price'])?></td>
  42.                                         <td><img src="<?php echo $fetch['product_image']?>" height="75px" height="75px"></td>
  43.                                 </tr>
  44.                                 <?php
  45.                                         }
  46.                                 ?>
  47.                         </tbody>
  48.                 </table>
  49.         </div>
  50.         <div class="modal fade" id="form_modal" aria-hidden="true">
  51.                 <div class="modal-dialog">
  52.                         <div class="modal-content">
  53.                                 <form method="POST" action="save_product.php" enctype="multipart/form-data">
  54.                                         <div class="modal-header">
  55.                                                 <h3 class="modal-title">Add Product</h3>
  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>Product Name</label>
  62.                                                                 <input type="text" name="product_name" class="form-control" required="required"/>
  63.                                                         </div>
  64.                                                         <div class="form-group">
  65.                                                                 <label>Product Price</label>
  66.                                                                 <input type="number" name="product_price" class="form-control" required="required"/>
  67.                                                         </div>
  68.                                                         <div class="form-group">
  69.                                                                 <label>Image</label>
  70.                                                                 <input type="file" name="product_image" 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 PHP Query

This code contains the php query of the application. This code will store the data inputs to the SQLite database thru PDO validation. To do this just copy and write these block of codes as shown below inside the text editor and save it as save_product.php.
  1. <?php
  2.         require_once 'conn.php';
  3.         if(ISSET($_POST['save'])){
  4.                 $product_name = $_POST['product_name'];
  5.                 $product_price = $_POST['product_price'];
  6.                 $product_image = $_FILES['product_image'];
  7.                
  8.                 $file_name = $product_image['name'];
  9.                 $file_temp = $product_image['tmp_name'];
  10.                 $file_size = $product_image['size'];
  11.                
  12.                 if($file_size < 2000000){
  13.                         $file = explode('.', $file_name);
  14.                         $end = end($file);
  15.                         $ext = array('jpg', 'png', 'jpeg');
  16.                         if(in_array($end, $ext)){
  17.                                 $name = time().date("Ymd");
  18.                                 $location = 'upload/'.$name.".".$end;
  19.                                 if(move_uploaded_file($file_temp, $location)){
  20.                                         $query = "INSERT INTO `product` (product_name, product_price, product_image) VALUES(:product_name, :product_price, :product_image)";
  21.                                         $stmt = $conn->prepare($query);
  22.                                         $stmt->bindParam(':product_name', $product_name);
  23.                                         $stmt->bindParam(':product_price', $product_price);
  24.                                         $stmt->bindParam(':product_image', $location);
  25.                                         $stmt->execute();
  26.                                        
  27.                                         $conn=null;
  28.                                         echo "<script>alert('Data Inserted')</script>";
  29.                                         echo "<script>window.location = 'index.php'</script>";
  30.                                 }
  31.                         }else{
  32.                                 echo "<script>alert('Wrong image format')</script>";
  33.                                 echo "<script>window.location = 'index.php'</script>";
  34.                         }
  35.  
  36.                 }else{
  37.                         echo "<script>alert('File too large to upload')</script>";
  38.                         echo "<script>window.location = 'index.php'</script>";
  39.                 }
  40.                
  41.         }
  42. ?>

Creating the Main Function

This code contains the main function of the application. This code will filter the data items base on the keyword entered. To do this just copy and write these block of codes as shown below inside the text editor and save it as search.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 - Simple Search Filter To SQLite Using PDO</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Product</button>
  18.                 <form method="POST" class="form-inline pull-right">
  19.                         <label>Search:</label>
  20.                         <input type="text" name="keyword" class="form-control" placeholder="Enter Product Name..." required="required"/>
  21.                         <button class="btn btn-success" name="search"><span class="glyphicon glyphicon-search"></span> Search</button>
  22.                 </form>
  23.                 <br /><br />
  24.                 <table class="table table-bordered">
  25.                         <thead class="alert-info">
  26.                                 <tr>
  27.                                         <th>Product Name</th>
  28.                                         <th>Product Price</th>
  29.                                         <th>Image</th>
  30.                                 </tr>
  31.                         </thead>
  32.                         <tbody style="background-color:#fff;">
  33.                                 <?php
  34.                                         if(ISSET($_POST['search']) && ISSET($_POST['keyword'])){
  35.                                                 $keyword = "%".$_POST['keyword']."%";
  36.                                                 require 'conn.php';
  37.                                                 $query = "SELECT * FROM `product` WHERE `product_name` LIKE :product_name ORDER BY `product_name` ASC";
  38.                                                 $stmt = $conn->prepare($query);
  39.                                                 $stmt->bindParam(':product_name', $keyword);
  40.                                                 $stmt->execute();
  41.                                                 while($fetch = $stmt->fetch()){
  42.                                 ?>
  43.                                 <tr>
  44.                                         <td><?php echo $fetch['product_name']?></td>
  45.                                         <td><?php echo number_format($fetch['product_price'])?></td>
  46.                                         <td><img src="<?php echo $fetch['product_image']?>" height="75px" height="75px"></td>
  47.                                 </tr>
  48.                                 <?php
  49.                                                 }
  50.                                         }
  51.                                 ?>
  52.                         </tbody>
  53.                 </table>
  54.         </div>
  55.         <div class="modal fade" id="form_modal" aria-hidden="true">
  56.                 <div class="modal-dialog">
  57.                         <div class="modal-content">
  58.                                 <form method="POST" action="save_product.php" enctype="multipart/form-data">
  59.                                         <div class="modal-header">
  60.                                                 <h3 class="modal-title">Add Product</h3>
  61.                                         </div>
  62.                                         <div class="modal-body">
  63.                                                 <div class="col-md-2"></div>
  64.                                                 <div class="col-md-8">
  65.                                                         <div class="form-group">
  66.                                                                 <label>Product Name</label>
  67.                                                                 <input type="text" name="product_name" class="form-control" required="required"/>
  68.                                                         </div>
  69.                                                         <div class="form-group">
  70.                                                                 <label>Product Price</label>
  71.                                                                 <input type="number" name="product_price" class="form-control" required="required"/>
  72.                                                         </div>
  73.                                                         <div class="form-group">
  74.                                                                 <label>Image</label>
  75.                                                                 <input type="file" name="product_image" required="required"/>
  76.                                                         </div>
  77.                                                 </div>
  78.                                         </div>
  79.                                         <div style="clear:both;"></div>
  80.                                         <div class="modal-footer">
  81.                                                 <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  82.                                                 <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  83.                                         </div>
  84.                                 </form>
  85.                         </div>
  86.                 </div>
  87.         </div>
  88. <script src="js/jquery-3.2.1.min.js"></script>
  89. <script src="js/bootstrap.js"></script>
  90. </body>
  91. </html>
There you have it we successfully created a Simple Search Filter 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

Thank you for the SQLite tutorials. They come in very useful.

Add new comment