Search field to filter data in a table

here is my code Kindly assist

<?php 
//session already set in config file
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

include('config/db.php');

if (isset($_POST['search'])) {
  
  $searchValue = $_POST['searchValue'];

  $sql = "SELECT * FROM tbl_donor WHERE CONCAT('id','firstname','mobilenumber','email','age','gender','gender','bloodgroup','location','posting_date' ) LIKE '%".$searchValue."%'";
    
}else{

$sql = "SELECT * FROM tbl_donor";
    
$sql = $dbh -> prepare($sql);

$sql->execute();

$searchValue = $sql ->fetchAll(PDO::FETCH_OBJ);

}

?>

<?php include_once 'includes/header.php' ?>
      
      <div id="header"> 
          <div class="logo"><a href="index.php"><span>BLOOD BANK </span> MANAGEMENT SYSTEM</a></div>
      </div>

      <!-- <a class="mobile" href="#">MAIN MENU</a> activate if on mobile view-->

      <div id="container">
        
        <div class="sidebar">
          <ul id="nav">

              <li><a class="selected">NAVIGATION MENU</a></li>
              <li><a href="index.php">Dashboard</a></li>
              <li><a href="manage_blood.php">Manage Blood Group</a></li>
              <li><a href="donor.php">Add Donor</a></li>
              <li><a href="acceptor.php">Add Acceptor</a></li>
              <li><a href="manage_users.php" >Manage Users</a></li>
              <li><a href="cpassword.php">Change Password</a></li>
              <li><a href="logout.php">Logout</a></li>
          </ul>
        </div><!-- end of sidebar -->
       

        <div class="content">

        <h2>Registered Donors</h2>

      <form class="filter" action="" method="POST">
             <div class="wrap">
               <div class="search">
                  <input type="text" name="searchValue" class="searchTerm"  placeholder="Search....">
                  <button type="submit" name="search" class="searchButton">
                      <img <img src="assests/img/search-icon.png" style="width: 35px; height: 35px;">
                 </button>
               </div>
            </div>
   

            <br>
             <br>
        <div class="table-wrapper">
        <table class="f2-table">
               <thead>
                    <tr>
                      <th>Id.</th>
                      <th>Full Name</th>
                      <th>Mobile</th>
                      <th>Email ID</th>
                      <th>Age</th>
                      <th>Gender</th>
                      <th>Blood Type</th>
                      <th>Location</th>
                      <th>Date Donated</th>
                    </tr> 
                </thead>
                <tbody>
                   <?php foreach($searchValue as $result): ?>
                    <tr>
                    <td><?= $result->id; ?></td>
                    <td><?= $result->fullname; ?></td>
                    <td><?= $result->mobilenumber; ?></td>
                    <td><?= $result->email; ?></td>
                    <td><?= $result->age; ?></td>
                    <td><?= $result->gender; ?></td>
                    <td><?= $result->bloodgroup; ?></td>
                    <td><?= $result->location; ?></td>
                    <td><?= $result->posting_date; ?></td>
                  
                    </tr>
                </tbody>
                   <?php endforeach; ?>
             </table>
      </form>
     </div>
       
            

        </div><!-- end of content -->

 </div><!-- end of container -->

<?php include_once 'includes/footer.php';?>

Assist what? Ask a question.

1 Like

With this part of query you could to have a problems…

WHERE CONCAT('id','firstname','mobilenumber','email','age','gender','gender','bloodgroup','location','posting_date' ) LIKE '%".$searchValue."%'"

And actually you get one “gender” more.

Better replace it with OR or UNION or at least CONCAT_WS().

I use an AJAX search very similar to the above and find the results van be incorrect because the fields are concatenated. The search term may be true and incorrect if part of the search term contains characters from the previous and next fields.

Your code only seems to execute the search query if the user doesn’t submit the form.

I also don’t like that method of concatenating fields to apply the search to any of them. Apart from anything else, it’s going to provide false positives as @John_Betong said.

1 Like

Why don’t you use DataTables plugin with ajax server side processing?