How to create search option or feature using PHP 7

How to create search option or feature using PHP 7

1. database.sql (SQL file for creating the table)

CREATE TABLE `user_data` ( `id` INT(6) NOT NULL AUTO_INCREMENT, `name` VARCHAR(40) DEFAULT NULL, `email` VARCHAR(40) DEFAULT NULL, `roll_no` VARCHAR(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. database.php (For connecting to the database)

<?php // Disable error reporting error_reporting(0); // Database connection $conn = mysqli_connect("localhost", "root", "", "student"); // Check if connection was successful if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // Handle form submission and search functionality $result = []; if (isset($_POST['save'])) { $roll_no = mysqli_real_escape_string($conn, $_POST['roll_no']); // Sanitize input $result = mysqli_query($conn, "SELECT * FROM user_data WHERE roll_no = '$roll_no'"); } ?>

3. search.php (HTML for search feature)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Search</title> <!-- Bootstrap CSS for styling --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css"> </head> <body> <div class="container mt-5"> <h2 class="text-center">Search Student</h2> <!-- Search Form --> <form class="form-inline mb-4" method="post" action="search.php"> <input type="text" name="roll_no" class="form-control text-center" placeholder="Search Roll No..." required> <button type="submit" name="save" class="btn btn-primary ml-2">Search</button> </form> <!-- Display search results in a table --> <?php if (isset($result) && mysqli_num_rows($result) > 0): ?> <table class="table table-striped table-bordered table-sm text-center" cellspacing="0" width="100%"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Roll No</th> </tr> </thead> <tbody> <?php while ($row = mysqli_fetch_array($result)): ?> <tr> <td><?= htmlspecialchars($row['id']); ?></td> <td><?= htmlspecialchars($row['name']); ?></td> <td><?= htmlspecialchars($row['email']); ?></td> <td><?= htmlspecialchars($row['roll_no']); ?></td> </tr> <?php endwhile; ?> </tbody> </table> <?php elseif (isset($result)): ?> <p class="text-danger">No results found for this roll number.</p> <?php endif; ?> <!-- Close database connection --> <?php mysqli_close($conn); ?> </div> <!-- Bootstrap JS (Optional for interactive features like modals) --> <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js"></script> <script> $(document).ready(function() { // Initialize DataTable $('table').DataTable(); }); </script> </body> </html>

Explanation:

  1. SQL Table (user_data):

    • The table has columns for id, name, email, and roll_no.

    • The id is set to AUTO_INCREMENT to ensure unique IDs for each row.

  2. database.php:

    • This file connects to the MySQL database and includes the logic for handling the search functionality.

    • It sanitizes user input for the roll_no field using mysqli_real_escape_string to prevent SQL injection.

    • The result of the query is stored in the $result variable and used in the HTML to display the search results.

  3. search.php:

    • The page contains a simple form that lets the user search by roll_no.

    • If the search finds results, they are displayed in a Bootstrap-styled table.

    • DataTables is used to enhance the table with features like sorting and searching within the table.

    • If no results are found, a message is displayed.

This setup allows for a basic search feature for user_data, and you can extend this to include more fields or features as needed.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close