[Solved] How to put search box in the database table using PHP and Mysql


There are a few things you will need to know to be able to do this. Firstly the security bit…

Basically you want to never trust data that is submitted to your application. When accepting data for use in a MySQL statement, you could use PHP’s built in escape functions for MySQL.
ref: http://php.net/manual/en/mysqli.real-escape-string.php

You already seem to know how to get data from the submitted form as I see you accessed the $_POST superglobal. For your search, you will do something similar. It would be something like $_POST[‘search’] where you are getting data posted from a form with a text element with the name “search”.

You will need an SQL query to search for results. Without seeing you database schema it’s hard to say for user, but I suspect this would work.

$search = mysqli_real_escape_string($_POST['search']);
$query= "
    SELECT * 
    FROM customers 
    WHERE 
        Firstname LIKE '%{$search}%'
        OR Lastname LIKE '%{$search}%'
        OR Email LIKE '%{$search}%'
";
$results = mysqli_query($conn,$query);

Once you have the results, you should be able to display them the same way you did with the example you gave.

Hope this helps!

solved How to put search box in the database table using PHP and Mysql