When I search null or space values, all the results from database come to my html table. I wanted to prevent users to search null values or space values.
I tried this post, but now helped me, How to prevent a database search from running on an empty string?
Here is the backend script-
<?php
//fetch.php
$connect = new PDO("mysql:host=localhost;dbname=searchv3", "root", "");
$output = '';
$query = '';
$data = [];
if(isset($_POST["query"]))
{
 $search = str_replace(",", "|", $_POST["query"]);
 $query = "
 SELECT * FROM number_list
 WHERE IMSI REGEXP '".$search."' 
 OR Mobile_no REGEXP '".$search."' 
 OR Backup_date REGEXP '".$search."' 
 OR Sr REGEXP '".$search."' 
 
 ";
}
else
{
 $query = "
 SELECT * FROM number_list order by Sr DESC LIMIT 50;
 ";
}
$statement = $connect->prepare($query);
$statement->execute();
while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
 $data[] = $row;
 
}
echo json_encode($data);
$connect = null;
?>
I will share front-end jav script as well-
<script>
$(document).ready(function(){
 load_data();
function load_data(query)
 {
  $.ajax({
   url:"fetch_numberlist.php",
   method:"POST",
   data:{query:query},
   dataType:"json",
   success:function(data)
   {
    $('#total_records').text(data.length);
    var html = '';
    if(data.length > 0)
        
    {
     for(var count = 0; count < data.length; count++)
     {
      html += '<tr>';
      html += '<td>'+data[count].Sr+'</td>';
      html += '<td>'+data[count].IMSI+'</td>';
      html += '<td>'+data[count].Mobile_no+'</td>';
      html += '<td>'+data[count].Backup_date+'</td>';
     }
    }
    else
    {
            html = '<tr><td colspan="4">No Data Found</td></tr>';
    }
    $('tbody').html(html);
   }
  })
 }
 $('#search').click(function(){
  var query = $('#tags').val();
  load_data(query);
 });
 })
</script>
 
     
    