"how to put search box in the database table using php and mysql" Code Answer


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 
        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!

By Cyril Gupta on August 10 2022

Answers related to “how to put search box in the database table using php and mysql”

Only authorized users can answer the Search term. Please sign in first, or register a free account.