Friday, 28 December 2018

Simple search form in php

Using This code you can fetching data from mysql.
mysql data and table details :
hostname : localhost
dbusername : dbusername
dbpassword : yourpassword
dbname : dbname
Table Details :
Table 1 : table_users ( column name : pk_i_id , name , username , password  )


Just copy and paste this code and modify mysql and table details as per your details

<?php

if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    // search in all table columns
    // using concat mysql function
    $query = "SELECT * FROM `table_user` WHERE CONCAT(`pk_i_id`, `s_name`, `s_username`, `s_password`) LIKE '%".$valueToSearch."%'";
    $search_result = filterTable($query);
 
}
 else {
    $query = "SELECT * FROM `osoh_t_user`";
    $search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
    $connect = mysqli_connect("localhost", "dbusername", "yourpassword", "dbname");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}

?>

<!DOCTYPE html>
<html>
    <head>
        <title>PHP HTML TABLE DATA SEARCH</title>
        <style>
            table,tr,th,td
            {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
     
        <form action="1d.php" method="post">
            <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
            <input type="submit" name="search" value="Filter"><br><br>
         
            <table>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Username</th>
                    <th>Password</th>
                </tr>

      <!-- populate table from mysql database -->
                <?php while($row = mysqli_fetch_array($search_result)):?>
                <tr>
                    <td><?php echo $row['pk_i_id'];?></td>
                    <td><?php echo $row['s_name'];?></td>
                    <td><?php echo $row['s_username'];?></td>
                    <td><?php echo $row['s_password'];?></td>
                </tr>
                <?php endwhile;?>
            </table>
        </form>
     
    </body>
</html>

No comments:

Post a Comment