Jump to Main Content

Creating a search feature PHP & MySQL

Developing a robust, interactive and engaging Web site involves many different avenues, such as interactive pop-out menu’s using dynamic JavaScript, Cascading Style Sheets (CSS), complex maps that allows visitors to rollover individual sections for detailed information, forms designed and formatted with CSS and are programmed to collect and send visitor feedback to a specified recipient. Other features could include: database driven pages that display a current member directory, a customized blog section that enables administrators to manage postings and allow random users in coordination with CAPTCHA techniques to post remarks to an article. Arguably, one of the most popular features of any database driven site is a searchable form feature that allows anyone to search for current staff members of an organization and find additional information, such as their email address or phone number.

In this article, you'll learn how to create a searchable form feature that will query a database table and display current staff member information. During the analysis you'll learn how to do the following: create a database table that will hold current staff listings, create a search form and use PHP, in coordination with Structured Query Language (SQL) to capture information entered by the visitor and append the information to display the results we want to show. Additionally, you'll learn about design patterns with the searchable form including security through code to ensure that only certain input is entered before performing operations on your database table and ways to pass state information through the address bar to display additional information about staff members.

Printable Version
Word PDF
Download Project Files
Zip

*Note: The numbers in brackets indicate a web address that is given at the end of this article.

What you’ll need

In order to complete this article, you’ll need the following tools and technologies:

  • One My SQL database
  • PHP support
  • A text editor

Creating our database

If you're unsure how to create a database through your host, please contact your system administrator for further information. Once you've created a database, you'll need a way to connect to it, create a table, and enter data for appropriate entities. One popular database administration tool for My SQL is PHP My Admin, or one that I'm particular fond of is SQLyog, which currently supports a free version (Community Edition) which is sufficient for the purposes of this article.

Creating our table

Our table needs to be created with the following columns and their corresponding data types and lengths:

Column Name Data Type Length Null or Not Null Primary key Auto increment
ID INT 1 Not Null Yes Yes
FirstName VarChar 50 Not Null No No
LastName VarChar 50 Not Null No No
Email VarChar 50 Not Null No No
PhoneNumber VarChar 15 Not Null No No

Create the form

In order to create the search form, you'll need to open a text editor of your choice. One that is free is PSPad. You can use any text editor you want; though it's recommended to choose one that has syntax highlighting to make PHP debugging and programming a bit easier. When creating the search form page, make sure to save it with a .php extension, otherwise, the PHP code will not execute. Once the form is saved, begin with the following markup:

<!DOCTYPE  HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta  http-equiv="Content-Type" content="text/html;  charset=iso-8859-1">
<title Search  Contacts/title>
</head>
<body>
<h3> Search  Contacts Details</h3>
<pYou may search either by first or last name</p>
<form  method="post" action="search.php?go"  id="searchform">
<input  type="text" name="name">
<input  type="submit" name="submit" value="Search">
</form>
</body>
</html>

If you're familiar with HTML, everything should look familiar until you reach the opening form tag. Inside the opening form tag, the key line of code is the action attribute. We set the action of the form to the name of our file and then append on a query string of "go". We'll discuss why this is important in the next section.

Search Start

We'll continue with the article with checking criteria.

top of page