Search code examples
phpmysqldatabasesearch-form

Why show all data from mysql databese if I hit saarch button by keep search field blank and need extra one HTML button for all Entries.?


When i keep search field blank and hit on search button, then show all results from mysql database, Why.... here is my php code....

I want to create it, when i keep search field blank and click search button... have to show error "no search result" and want to create disallowed white spacing search and need extra one HTML button for all Entries. By one click so that i get all entries......

please help me....

 <?php

$con=mysql_connect('localhost', '1093913', 'tanim1996');
$db=mysql_select_db('1093913');



if(isset($_POST['button'])){    //trigger button click

  $search=$_POST['search'];

  $query=mysql_query("select * from iconic19 where student_id like '%{$search}%' || name like '%{$search}%' || phone like '%{$search}%' || blood like '%{$search}%' || district like '%{$search}%' ");



if (mysql_num_rows($query) > 0) {                 
  while ($row = mysql_fetch_array($query)) {

 echo "<tbody>";
               echo "<tr>";
                echo "<td data-label='Student ID'>".$row['student_id']."</td>";
               echo "<td data-label='Name' style='font-weight:bold;' >".$row['name']."</td>";
                 echo "<td data-label='Mobile No'>"."<a href='tel:".$row['phone']."'>".$row['phone']."</a>"."</td>";
                echo "<td data-label='Blood' style='color:red; font-weight:bold;' >".$row['blood']."</td>";
                echo "<td data-label='Email'>"."<a href='mailto:".$row['email']."'>".$row['email']."</a>"."</td>";
                echo "<td data-label='District'>".$row['district']."</td>";
                echo "</tr>";
echo "</tbody>";

  }
}else{
    echo "<div class='error-text'>No results</div><br><br>";
  }

}else{                          //while not in use of search  returns all the values
  $query=mysql_query("select * from iconic19");

 while ($row = mysql_fetch_array($query)) {



  }


}

mysql_close();
?>

Its Html Code

<form id="nbc-searchblue1" method="post" enctype="multipart/form-data" autocomplete="off">

    <input id='wc-searchblueinput1' placeholder="Search Iconic..." name="search" type="search" autofocus>
    <br>
    <input id='nbc-searchbluesubmit1' value="Search" type="submit" name="button">

   <div class="view-all"> <a href="script.php">Show all</a></div>

</form>

Its css Code..

.view-all a {
    background: red;
    padding: 10px;
    border-radius: 4px;
    color: #fff;
    text-decoration: none;
}

Solution

  • <?php
    
    $con=mysql_connect('localhost', '1093913', 'tanim1996');
    $db=mysql_select_db('1093913');
    
    if(isset($_POST['button'])){    //trigger button click
        $numRows = 0;
        if(!empty($_POST['search'])) {
            $search = mysql_real_escape_string($_POST['search']);
            $query = mysql_query("select * from iconic19 where student_id like '%{$search}%' || name like '%{$search}%' || phone like '%{$search}%' || blood like '%{$search}%' || district like '%{$search}%' ");
            $numRows = (int)mysql_num_rows($query);
        }
    
        if ($numRows > 0) {                 
            while ($row = mysql_fetch_array($query)) {
    
                echo "<tbody>";
                echo "<tr>";
                echo "<td data-label='Student ID'>".$row['student_id']."</td>";
                echo "<td data-label='Name' style='font-weight:bold;' >".$row['name']."</td>";
                echo "<td data-label='Mobile No'>"."<a href='tel:".$row['phone']."'>".$row['phone']."</a>"."</td>";
                echo "<td data-label='Blood' style='color:red; font-weight:bold;' >".$row['blood']."</td>";
                echo "<td data-label='Email'>"."<a href='mailto:".$row['email']."'>".$row['email']."</a>"."</td>";
                echo "<td data-label='District'>".$row['district']."</td>";
                echo "</tr>";
                echo "</tbody>";
    
            }
        } else {
            echo "<div class='error-text'>No results</div><br><br>";
        }
    } else {                          //while not in use of search  returns all the values
        $query = mysql_query("select * from iconic19");
    
        while ($row = mysql_fetch_array($query)) {
    
    
    
        }
    }
    
    mysql_close();
    
    ?>
    

    What I have done was creating a new variable $numRows with a default value of 0. If your search is empty there is no query to the database. I escaped your $search variable.

    BTW: Please change to mysqli, the mysql extension is no longer supported in newer php versions.