Skip to main content

PHP - Simple Search Record

In this tutorial we will create a Simple Search Record using PHP. This code will search a specific data in the MySQLi server when the user entered a keyword. The code use MySQLi where like function to initiate a search query to display a list of data that based on a keyword. This is a user-friendly kind of program feel free to modify it and use it as your own.

We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

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 bootstrap that i used for the layout design

Creating Database

Open your database web server then create a database name in it db_record, after that click Import then locate the database file inside the folder of the application then click ok.

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 = mysqli_connect("localhost", "root", "", "db_record");
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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 your 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 - Simple Search Record</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form method="POST" action="add.php">
  18. <div class="form-inline">
  19. <input type="text" name="book" class="form-control" placeholder="Book" required="required"/>
  20. <input type="text" name="author" class="form-control" placeholder="Author" required="required"/>
  21. <button class="btn btn-primary" name="add"><span class="glyphicon glyphicon-save"></span> Save Book</button>
  22. </div>
  23. </form>
  24. <br />
  25. <form method="POST" action="">
  26. <div class="form-inline pull-right">
  27. <input type="text" class="form-control" name="keyword" placeholder="Search here..." required="required"/>
  28. <button class="btn btn-success" name="search"><span class="glyphicon glyphicon-search"></span> Search</button>
  29. </div>
  30. </form>
  31. <br /><br />
  32. <?php include 'search_book.php'?>
  33. </body>
  34. </html>

Creating the PHP Query

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

  1. <?php
  2. require_once 'conn.php';
  4. if(ISSET($_POST['add'])){
  5. $book = $_POST['book'];
  6. $author = $_POST['author'];
  8. mysqli_query($conn, "INSERT INTO `book` VALUES('', '$book', '$author')") or die(mysqli_error());
  9. echo "<script>alert('Book Saved!')</script>";
  10. echo "<script>window.location = 'index.php'</script>";
  11. }
  12. ?>

Creating the Main Function

This code contains the main function of the application. This code will search a specific data in the database when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as search_book.php.

  1. <?php
  2. require 'conn.php';
  3. if(ISSET($_POST['search'])){
  4. ?>
  5. <table class="table table-striped">
  6. <thead class="alert-info">
  7. <tr>
  8. <th>Book</th>
  9. <th>Author</th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <?php
  14. $keyword = $_POST['keyword'];
  15. $query = mysqli_query($conn, "SELECT * FROM `book` WHERE `title` LIKE '%$keyword%' or `author` LIKE '%$keyword%'") or die(mysqli_error());
  16. $count = mysqli_num_rows($query);
  17. if($count > 0){
  18. while($fetch = mysqli_fetch_array($query)){
  19. ?>
  20. <tr>
  21. <td><?php echo $fetch['title']?></td>
  22. <td><?php echo $fetch['author']?></td>
  23. </tr>
  24. <?php
  25. }
  26. }else{
  27. echo "<tr><td colspan='2' class='text-danger'><center>No result found!</center></td></tr>";
  28. }
  29. ?>
  30. </tbody>
  31. </table>
  32. <?php
  33. }else{
  34. ?>
  35. <table class="table table-striped">
  36. <thead class="alert-info">
  37. <tr>
  38. <th>Book</th>
  39. <th>Author</th>
  40. </tr>
  41. </thead>
  42. <tbody>
  43. <?php
  44. $query = mysqli_query($conn, "SELECT * FROM `book` ORDER BY `title` ASC") or die(mysqli_error());
  45. while($fetch = mysqli_fetch_array($query)){
  46. ?>
  47. <tr>
  48. <td><?php echo $fetch['title']?></td>
  49. <td><?php echo $fetch['author']?></td>
  50. </tr>
  51. <?php
  52. }
  53. ?>
  54. </tbody>
  55. </table>
  56. <?php
  57. }
  58. ?>

There you have it we successfully created Simple Search Record using jQuery. 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 byIsacha (not verified)on Mon, 06/01/2020 - 01:15

Razomist... Nice one but how do I implement a search for five or six tables from the database?

Add new comment

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