PHP - Simple Search Record

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

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 https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

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.
tut1

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");
  3.  
  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="<a href="https://sourceodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourceodester.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 - Simple Search Record</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <form method="POST" action="add.php">
  19. <div class="form-inline">
  20. <input type="text" name="book" class="form-control" placeholder="Book" required="required"/>
  21. <input type="text" name="author" class="form-control" placeholder="Author" required="required"/>
  22. <button class="btn btn-primary" name="add"><span class="glyphicon glyphicon-save"></span> Save Book</button>
  23. </div>
  24. </form>
  25. <br />
  26. <form method="POST" action="">
  27. <div class="form-inline pull-right">
  28. <input type="text" class="form-control" name="keyword" placeholder="Search here..." required="required"/>
  29. <button class="btn btn-success" name="search"><span class="glyphicon glyphicon-search"></span> Search</button>
  30. </div>
  31. </form>
  32. <br /><br />
  33. <?php include 'search_book.php'?>
  34. </body>
  35. </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';
  3.  
  4. if(ISSET($_POST['add'])){
  5. $book = $_POST['book'];
  6. $author = $_POST['author'];
  7.  
  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!


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.