PHP and MySQL Web Development - P52: PHP and MySQL Web Development teaches the reader to develop dynamic, secure, commercial Web sites. Using the same accessible, popular teaching style of the first edition, this best-selling book has been updated to reflect the rapidly changing landscape of MySQL and PHP. | Checking and Filtering Input Data 227 Figure The results of searching the database for books about Java are presented in a Web page using the script. The Basic Steps in Querying a Database from the Web In any script used to access a database from the Web you will follow some basic steps 1. Check and filter data coming from the user. 2. Set up a connection to the appropriate database. 3. Query the database. 4. Retrieve the results. 5. Present the results back to the user. These are the steps we have followed in the script and we will go through each of them in turn. Checking and Filtering Input Data We begin our script by stripping any whitespace that the user might have inadvertently entered at the beginning or end of his search do this by applying the function trim to searchterm. searchterm trim searchterm Our next step is to verify that the user has entered a search term and search type. Note that we check he entered a search term after trimming whitespace from the ends of searchterm. Had we arranged these lines in the opposite order we could get 228 Chapter 10 Accessing Your MySQL Database from the Web with PHP situations where a user s search term was not empty so it did not create an error message but it was all whitespace so it was deleted by trim if searchtype searchterm echo You have not entered search details. Please go back and try again. exit You will notice that we ve checked the searchtype variable even though in this case it s coming from an HTML might ask why we bother checking data that has to be filled in. It s important to remember that there might be more than one interface to your database. For example Amazon has many affiliates who use their search interface. Also it s sensible to screen data in case of any security problems that can arise because of users coming from different points of entry. Also when you are going to use any data input by a user it is important to filter it appropriately for .