An Introduction to PHP Syntax and Code Construction

Adding Records to Your Database Using PHP, Part 1: The Insert Form

Adding Records to Your Database Using PHP, Part 2: The PHP Script

An Introduction to MySQL

A Simple Search Engine Part 1: The Search Form

A Simple Search Engine, Part 2: The PHP Script

MySQL: Creating a Table and Inserting a Record

How does Dynamic, Database-Driven Design work?

   

HomeTutorials: A Simple Search Engine Part 1: The Search Form

A Simple Search Engine Part 1: The Search Form

Introduction

This lesson is the first of two tutorials that will examine how to create a simple search engine using MySQL and PHP. In Part 1, we will look at the code needed to allow the user to enter and submit a search request. In Part 2, we will deconstruct a PHP script that queries a MySQL database.

Setting Up a Search Form

For those familiar with HTML, the code for a search form that connects to your PHP script will look very familiar. Here, for example, is the search form for the 4D web site:

‹form action="results.php" method="post"› Choose Search Type:

‹br›

‹select name="formdata"›

  ‹option value="glossary"›Glossary

  ‹option value="overviews"›Overviews

  ‹option value="reviews"›Reviews

  ‹option value="tutorials"›Tutorials

‹/select›

‹p›‹br›

Enter Search Term:‹br›

‹input name="searchterm" type=text›

‹br›

   ‹input type=submit value="Search"›

‹/p›

‹/form›

As you can see, our search form includes a basic HTML drop down menu that allows the user to choose a section of the site (also tables in the 4D database) to search. The form also includes a text field where the search term or terms are entered and a standard submit button. More importantly, one should note:

  • The action point not to a CGI-script, as a typical form, but to a file with a .php extension called results.php. This file, which we will look at in the next tutorial, contains the script that will generate the results of the user’s search.
  • The select name of our drop down menu is formdata, which is a variable in our search results php script. The value of this variable is set by the user (glossary, for example).
  • The input name for the text field is searchterm, Again, this is a variable that is set to a value entered by the user, in this case a text string.

Looking Forward

With our search form written, the next steps will be to capture the data entered by the user, query the MySQL database to find records that match that information and return the results to the user. Read on to the next tutorial, A Simple Search Engine Part 2: The PHP Script to review these concepts.