Problem Solution Discussion Collecting Web Input
18.6 Collecting Web Input
18.6.1 Problem
You want t o ext ract t he input param et ers t hat were subm it t ed as part of a form or specified at t he end of a URL.18.6.2 Solution
Each API provides a m eans of accessing t he nam es and values of t he input param et ers in t he execut ion environm ent of a web script .18.6.3 Discussion
Earlier sect ions of t his chapt er discuss how t o ret rieve inform at ion from MySQL and use it t o generat e various form s of out put , such as st at ic t ext , hyperlinks, or form elem ent s. I n t his sect ion, w ell discuss t he opposit e problem —how t o collect input from t he Web. Applicat ions for such input are m any. For exam ple, you can use t he t echniques shown in t his sect ion t o ext ract t he cont ent s of a form subm it t ed by a user. You m ight int erpret t he inform at ion as search keywords, t hen run a query against a product cat alog and show t he m at ching it em s t o a cust om er. I n t his case, you use t he Web t o collect inform at ion from which you can det erm ine t he client s int erest s. From t hat you const ruct an appropriat e search query and display t he result s. I f a form represent s a survey, a m ailing list sign- up sheet , or a poll, you m ight j ust st ore t he values, using t he dat a t o creat e a new dat abase record or perhaps t o updat e an exist ing record . A script t hat receives input over t he Web and uses it t o int eract wit h MySQL generally processes t he inform at ion in a series of st ages: 1. Ext ract t he input from t he execut ion environm ent . When a request arrives t hat cont ains input param et ers, t he web server places t he input int o t he environm ent of t he script t hat handles t he request , and t he script queries it s environm ent t o obt ain t he param et ers. I t m ay be necessary t o decode special charact ers in t he param et ers t o recover t he act ual values subm it t ed by t he client , if t he ext ract ion m echanism provided by your API doesnt do it for you. For exam ple, you m ay need t o convert 20 t o space. 2. Validat e t he input t o m ake sure it s legal. You cannot t rust users t o send legal values, so it s a good idea t o check input param et ers t o m ake sure t hey look reasonable. For exam ple, if you expect a user t o ent er a num ber int o a field, you should check t he value t o be sure it s really num eric. I f a form cont ains a pop- up m enu t hat was const ruct ed using t he allowable values of an ENUM colum n, you m ight expect t he value t hat you act ually get back t o be one of t hese values. But t heres no way t o be sure except t o check. I f you dont , you run t he risk of ent ering garbage int o your dat abase. 3. Const ruct a query based on t he input . Typically, input param et ers are used t o add a record t o a dat abase, or t o t o ret rieve inform at ion from t he dat abase for display t o t he client . Eit her way, you use t he input t o const ruct a query and send it t o t he MySQL server. Query const ruct ion based on user input should be done wit h care, using proper escaping t o avoid creat ing m alform ed or dangerous SQL st at em ent s. The rest of t his sect ion explores t he first of t hese t hree st ages of input processing. Recipe 18.7Recipe 18.7 and Recipe 18.8 cover t he second and t hird st ages. The first st age pulling input from t he execut ion environm ent has lit t le t o do wit h MySQL, but is covered here because it s necessarily t he m eans by which you obt ain t he inform at ion used in t he lat er processing st ages. I nput obt ained over t he Web can be received in several ways, t wo of which are m ost com m on: • As part of a GET request , in which case input param et ers are appended t o t he end of t he URL. For exam ple, t he following URL invokes a PHP script price_quot e.php and specifies item and quantity param et ers w it h values D-0214 and 60 : ht t p: apache.snake.net m cb price_quot e.php?it em = D- 0214quant it y= 60 Such request s com m only are received when a user select s a hyperlink or subm it s a form t hat specifies method=GET in t he form t ag. A param et er list in a URL begins wit h ? and consist s of name = value pairs separat ed by ; or charact ers. I t s also possible t o place inform at ion in t he m iddle of a URL, but t his book doesnt cover t hat . • As part of a POST request , such as a form subm ission t hat specifies method=POST in t he form t ag. The cont ent s of a form for a POST request are sent as input param et ers in t he body of t he request , rat her t han at t he end of t he URL. You m ay also have occasion t o process ot her t ypes of input , such as uploaded files. Those are sent using POST request s, but as part of a special kind of form t hat is discussed in Recipe 18.9 . When you gat her input for a web script , you m ay need t o be concerned wit h how t he input was sent . Som e API s dist inguish bet ween input sent via GET and POST , ot her s do not . However, once you have pulled out t he inform at ion t hat was sent , t he request m et hod doesnt m at t er. The validat ion and query const ruct ion st ages do not need t o know whet her param et ers were sent using GET or POST . The recipes dist ribut ion includes som e script s in t he apache param s direct ory t om cat m cb for JSP t hat process input param et ers. Each script allows you t o subm it GET or POST request s, and shows how t o ext ract and display t he param et er values t hus subm it t ed. Exam ine t hese script s t o see how t he param et er ext ract ion m et hods for t he various API s are used. Ut ilit y rout ines invoked by t he script s can be found in t he library m odules in t he lib direct ory of t he dist ribut ion.18.6.4 Web Input Extraction Conventions
Parts
» O'Reilly-MySQL.Cookbook.eBook-iNTENSiTY. 4810KB Mar 29 2010 05:03:43 AM
» Introduction Using the mysql Client Program
» Problem Solution Discussion Setting Up a MySQL User Account
» Problem Solution Discussion Starting and Terminating mysql
» Problem Solution Discussion Specifying Connection Parameters by Using Option Files
» Problem Solution Discussion Mixing Command-Line and Option File Parameters
» Problem Solution Discussion What to Do if mysql Cannot Be Found
» Problem Solution Discussion Setting Environment Variables
» Problem Solution Discussion Repeating and Editing Queries
» Problem Solution Discussion Preventing Query Output from Scrolling off the Screen
» Problem Solution Discussion Specifying Arbitrary Output Column Delimiters
» Problem Solution Discussion Logging Interactive mysql Sessions
» Discussion Using mysql as a Calculator
» Writing Shell Scripts Under Unix
» Writing Shell Scripts Under Windows
» MySQL Client Application Programming Interfaces
» Perl Connecting to the MySQL Server, Selecting a Database, and Disconnecting
» PHP Connecting to the MySQL Server, Selecting a Database, and Disconnecting
» Python Connecting to the MySQL Server, Selecting a Database, and Disconnecting
» Java Connecting to the MySQL Server, Selecting a Database, and Disconnecting
» Problem Solution Discussion Checking for Errors
» Python Java Checking for Errors
» Problem Solution Discussion Writing Library Files
» Python Writing Library Files
» SQL Statement Categories Issuing Queries and Retrieving Results
» Perl Issuing Queries and Retrieving Results
» Python Issuing Queries and Retrieving Results
» Java Issuing Queries and Retrieving Results
» Problem Solution Discussion Moving Around Within a Result Set
» Problem Solution Discussion Using Prepared Statements and Placeholders in Queries
» Perl Using Prepared Statements and Placeholders in Queries
» PHP Python Java Using Prepared Statements and Placeholders in Queries
» Problem Solution Discussion Including Special Characters and NULL Values in Queries
» Perl Including Special Characters and NULL Values in Queries
» PHP Including Special Characters and NULL Values in Queries
» Python Java Including Special Characters and NULL Values in Queries
» PHP Python Java Handling NULL Values in Result Sets
» Problem Solution Discussion Writing an Object-Oriented MySQL Interface for PHP
» Class Overview Writing an Object-Oriented MySQL Interface for PHP
» Connecting and Disconnecting Writing an Object-Oriented MySQL Interface for PHP
» Error Handling Issuing Queries and Processing the Results
» Quoting and Placeholder Support
» Problem Solution Discussion Ways of Obtaining Connection Parameters
» Getting Parameters from the Command Line
» Getting Parameters from Option Files
» Conclusion and Words of Advice
» Problem Solution Discussion Avoiding Output Column Order Problems When Writing Programs
» Problem Solution Discussion Using Column Aliases to Make Programs Easier to Write
» Problem Solution Discussion Selecting a Result Set into an Existing Table
» Problem Solution Discussion Creating a Destination Table on the Fly from a Result Set
» Problem Solution Discussion Moving Records Between Tables Safely
» Problem Solution Discussion Cloning a Table Exactly
» Problem Solution Discussion Generating Unique Table Names
» Problem Solution Discussion Using TIMESTAMP Values
» Problem Solution Discussion Using ORDER BY to Sort Query Results
» Solution Discussion Working with Per-Group and Overall Summary Values Simultaneously
» Problem Solution Discussion Changing a Column Definition or Name
» Problem Solution Discussion Changing a Table Type
» Problem Solution Discussion Adding Indexes
» Introduction Obtaining and Using Metadata
» Problem Solution Discussion Perl PHP
» Problem Solution Discussion Perl
» PHP Obtaining Result Set Metadata
» Python Obtaining Result Set Metadata
» Java Obtaining Result Set Metadata
» Using Result Set Metadata to Get Table Structure
» Problem Solution Discussion Database-Independent Methods of Obtaining Table Information
» Problem Solution Discussion Displaying Column Lists Interactive Record Editing
» Mapping Column Types onto Web Page Elements Adding Elements to ENUM or SET Column Definitions
» Selecting All Except Certain Columns
» Problem Solution Discussion Listing Tables and Databases
» Problem Solution Writing Applications That Adapt to the MySQL Server Version
» Discussion Writing Applications That Adapt to the MySQL Server Version
» Problem Solution Discussion Determining Which Table Types the Server Supports
» General Import and Export Issues
» Problem Solution Discussion Importing Data with LOAD DATA and mysqlimport
» Problem Solution Discussion Specifying the Datafile Location
» Problem Solution Discussion Specifying the Datafile Format
» Problem Solution Discussion Dealing with Quotes and Special Characters
» Problem Solution Discussion Handling Duplicate Index Values
» Problem Solution Discussion Getting LOAD DATA to Cough Up More Information
» Problem Solution Discussion Dont Assume LOAD DATA Knows More than It Does
» Problem Solution Discussion Skipping Datafile Columns
» Problem Solution Discussion Exporting Query Results from MySQL
» Using the mysql Client to Export Data
» Problem Solution Discussion Exporting Tables as Raw Data
» Problem Solution Discussion Exporting Table Contents or Definitions in SQL Format
» Problem Solution Discussion Copying Tables or Databases to Another Server
» Problem Solution Discussion Writing Your Own Export Programs
» Problem Solution Discussion Converting Datafiles from One Format to Another
» Problem Solution Discussion Extracting and Rearranging Datafile Columns
» Problem Solution Discussion Validating and Transforming Data
» Writing an Input-Processing Loop Putting Common Tests in Libraries
» Problem Solution Discussion Validation by Pattern Matching
» Problem Solution Discussion Using Patterns to Match Numeric Values
» Problem Solution Discussion Using Patterns to Match Dates or Times
» See Also Using Patterns to Match Dates or Times
» Problem Solution Discussion Using Patterns to Match Email Addresses and URLs
» Problem Solution Discussion Validation Using Table Metadata
» Problem Solution Discussion Issue Individual Queries Construct a Hash from the Entire Lookup Table
» Use a Hash as a Cache of Already-Seen Lookup Values
» Problem Solution Discussion Converting Two-Digit Year Values to Four-Digit Form
» Problem Solution Discussion Performing Validity Checking on Date or Time Subparts
» Problem Solution Discussion Writing Date-Processing Utilities
» Problem Solution Discussion Performing Date Conversion Using SQL
» Problem Solution Discussion Guessing Table Structure from a Datafile
» Problem Solution Discussion A LOAD DATA Diagnostic Utility
» Problem Solution Discussion Exchanging Data Between MySQL and Microsoft Access
» Problem Solution Discussion Exchanging Data Between MySQL and Microsoft Excel
» Problem Solution Discussion Exchanging Data Between MySQL and FileMaker Pro
» Problem Solution Discussion Importing XML into MySQL
» Epilog Importing and Exporting Data
» Introduction Generating and Using Sequences
» Problem Solution Discussion Using AUTO_INCREMENT To Set Up a Sequence Column
» Problem Solution Discussion Choosing the Type for a Sequence Column
» Problem Solution Discussion Ensuring That Rows Are Renumbered in a Particular Order
» Problem Solution Discussion Managing Multiple Simultaneous AUTO_INCREMENT Values
» Problem Solution Discussion Using AUTO_INCREMENT Values to Relate Tables
» Problem Solution Discussion Generating Repeating Sequences
» Problem Solution Discussion See Also
» Performing a Related-Table Update Using Table Replacement
» Performing a Related-Table Update by Writing a Program
» Performing a Multiple-Table Delete by Writing a Program
» Problem Solution Discussion Dealing with Duplicates at Record-Creation Time
» Problem Solution Discussion Using Transactions in Perl Programs
» Problem Solution Discussion Using Transactions in Java Programs
» Problem Solution Discussion Using Alternatives to Transactions
» Grouping Statements Using Locks
» Rewriting Queries to Avoid Transactions
» Introduction Introduction to MySQL on the Web
» Problem Solution Discussion Basic Web Page Generation
» Problem Solution Discussion Using Apache to Run Web Scripts
» Problem Solution Discussion Using Tomcat to Run Web Scripts
» Installing the mcb Application
» Installing the JSTL Distribution
» Problem Solution Discussion Encoding Special Characters in Web Output
» General Encoding Principles Encoding Special Characters in Web Output
» Encoding Special Characters Using Web APIs
» Introduction Incorporating Query Results into Web Pages
» Problem Solution Discussion Creating a Navigation Index from Database Content
» Creating a Multiple-Page Navigation Index
» Problem Solution Discussion Storing Images or Other Binary Data
» Storing Images with LOAD_FILE Storing Images Using a Script
» Problem Solution Discussion Retrieving Images or Other Binary Data
» Problem Solution Discussion Serving Banner Ads
» Problem Solution Discussion Serving Query Results for Download
» Introduction Processing Web Input with MySQL
» Problem Solution Discussion Creating Forms in Scripts
» Problem Solution Discussion Creating Multiple-Pick Form Elements from Database Content
» Problem Solution Discussion Loading a Database Record into a Form
» Problem Solution Discussion Collecting Web Input
» Web Input Extraction Conventions Perl
» Problem Solution Discussion Validating Web Input
» Problem Solution Discussion Using Web Input to Construct Queries
» Problem Solution Discussion Processing File Uploads
» Perl Processing File Uploads
» Problem Solution Discussion Performing Searches and Presenting the Results
» Problem Solution Discussion Generating Previous-Page and Next-Page Links
» Paged Displays with Previous-Page and Next-Page Links
» Paged Displays with Links to Each Page
» Problem Solution Discussion Web Page Access Counting
» Problem Solution Discussion Web Page Access Logging
» Problem Solution Discussion Setting Up Database Logging
» Other Logging Issues Using MySQL for Apache Logging
» Session Management Issues Introduction
» Problem Solution Discussion Installing Apache::Session
» The Apache::Session Interface
» A Sample Application Using MySQL-Based Sessions in Perl Applications
» Problem Solution Discussion The PHP 4 Session Management Interface
» Specifying a User-Defined Storage Module
» Problem Solution Discussion Using MySQL for Session BackingStore with Tomcat
» The Servlet and JSP Session Interface A Sample JSP Session Application
Show more