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


    $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>
        <title>PHP HTML TABLE DATA SEARCH</title>
                border: 1px solid black;
        <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>

      <!-- populate table from mysql database -->
                <?php while($row = mysqli_fetch_array($search_result)):?>
                    <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>
                <?php endwhile;?>

No comments:

Post a Comment