18.1 Introduction
The previous chapt er describes how t o ret rieve inform at ion from MySQL and display it in web pages using various t ypes of HTML const ruct s such as t ables or hyperlinks. That s a use of
MySQL t o send inform at ion in one direct ion from web server t o user , but web-based dat abase program m ing can also serve t o collect inform at ion sent in t he ot her direct ion from
user t o web server, such as t he cont ent s of a subm it t ed form . I f youre processing a survey form , you m ight st ore t he inform at ion for lat er use. I f t he form cont ains search keywords,
youd use t hem as t he basis for a query t hat searches t he dat abase for inform at ion t he user want s t o see.
MySQL com es int o t hese act ivit ies in a fairly obvious way, as t he reposit ory for st oring inform at ion or as t he source from which search result s are drawn. But before you can process
input from a form , you have t o creat e t he form and send it t o t he user. MySQL can help wit h t his, t oo, because it s oft en possible t o use inform at ion st ored in your dat abase t o generat e
form elem ent s such as radio but t ons, checkboxes, pop- up m enus, or scrolling list s:
•
You can select a set of it em s from a t able t hat list s count ries, st at es, or provinces and convert t hem int o a pop- up m enu for use in a form t hat collect s address inform at ion.
•
You can use t he list of legal values for an
ENUM
colum n t hat cont ains allowable salut at ions Mr., Mrs., and so fort h t o generat e a set of radio but t ons.
•
You can use list s of available colors, sizes, or st yles st ored in an invent ory dat abase t o const ruct fields for a clot hing ordering form .
•
I f you have an applicat ion t hat allows t he user t o pick a dat abase or t able, you can run a
SHOW DATABASES
or
SHOW TABLES
query and use t he result ing nam es t o creat e a list elem ent .
By using dat abase cont ent t o generat e form elem ent s, you lessen t he am ount of t able- specific knowledge your program s m ust have and allow t hem t o det erm ine what t hey need
aut om at ically. A script t hat uses a dat abase t o figure out for it self how t o generat e form elem ent s adapt ively handles changes t o t he dat abase. To add a new count ry, creat e a new
row in t he t able t hat st ores t he list of count ries. To add a new salut at ion, change t he definit ion of t he
ENUM
colum n. I n each case, you change t he set of it em s in a form elem ent by updat ing t he dat abase, not by m odifying t he script ; t he script adapt s t o t he change
aut om at ically, wit hout addit ional program m ing. The first part of t his chapt er covers t he following t opics relat ing t o web input processing:
•
Ge ne r a t ing for m s a nd for m e le m e nt s.
One w ay t o use dat abase cont ent for form const ruct ion is by select ing a list of it em s from a t able and using t hem t o creat e t he opt ions in a list elem ent . But m et adat a can
be used as well. There is a nat ural correspondence bet ween
ENUM
colum ns and single-pick form elem ent s like radio but t on set s or pop- up m enus. I n bot h cases, only
one from a set of possible values m ay be chosen. There is a sim ilar correspondence bet w een
SET
colum ns and m ult iple-pick elem ent s like checkbox groups; any or all of t he possible values m ay be chosen. To const ruct m et adat a-based form elem ent s,
obt ain t he colum n descript ion from t he t able inform at ion ret urned by
SHOW COLUMNS
, ext ract t he set of possible values, and use t hem for t he it em s in t he form elem ent .
•
I n it ia lizin g for m s u sin g da t a ba se con t e n t s.
I n addit ion t o using t he dat abase t o creat e st ruct ural elem ent s of form s, you can also use it t o init ialize form field values. For exam ple, t o allow a user t o m odify an exist ing
record, ret rieve it from t he dat abase and load it int o an edit ing form s fields before sending t he form t o t he user.
•
Pr oce ssin g in pu t ga t h e r e d ove r t h e W e b.
This includes input not only from form fields, but also t he cont ent s of uploaded files, or param et ers t hat are present in URLs. Regardless of where t he inform at ion com es
from , youll face a com m on set of issues in dealing wit h it : ext ract ing and decoding t he inform at ion, perform ing const raint or validit y checking on it , and re- encoding t he
inform at ion for query const ruct ion t o avoid generat ing m alform ed queries or st oring inform at ion inaccurat ely.
The second part of t he chapt er illust rat es a few ways t o apply t he t echniques developed in t he first part . These include applicat ions t hat show how t o use MySQL t o present a web-based
search int erface, creat e paged displays t hat cont ain next - page and previous-page links, im plem ent per-page hit count ing and logging, and perform general Apache logging t o a
dat abase. For t he Perl, PHP, and Pyt hon exam ple script s discussed in t his chapt er, look under t he apache
direct ory of t he
recipes
dist ribut ion. For JSP, t he script s are under t he t om cat direct ory; you should already have inst alled t hese in t he process of set t ing up t he
mcb
applicat ion cont ext
Recipe 16.4 . Library rout ines used by t he exam ple script s are locat ed in files found
under t he lib direct ory. Script s t o creat e t he t ables used here are locat ed in t he t ables direct ory.
Not e t hat alt hough t he script s in t his chapt er are int ended t o be invoked from your browser aft er t hey have been inst alled, m any of t hem JSP pages except ed can also be invoked from
t he com m and line if you wish t o see t he raw HTML t hey produce. To provide a concret e cont ext for discussion, m any of t he form -processing exam ples in t his
chapt er are based on t he following scenario: in t he lucrat ive field of const ruct - a-cow business endeavors, you run an operat ion t hat m anufact ures build-t o- order ceram ic bovine
figurines, and you want t o design an online ordering applicat ion t hat let s cust om ers m ake
select ions for several aspect s of t he product . For each order, it s necessary t o collect several t ypes of inform at ion:
•
Cow color .
The part icular list of colors available at any part icular t im e changes occasionally, so for flexibilit y, t he values can be st ored in a dat abase t able. To change t he set of colors
t hat cust om ers can choose from , j ust updat e t he t able.
•
Cow size .
There is a fixed set of sizes t hat doesnt change oft en sm all, m edium , large , so t he values can be represent ed as elem ent s of an
ENUM
colum n.
•
Th e a ll- im por t a n t cow a cce ssor y it e m s.
These include a bell, horns, a sport y- looking t ail ribbon, and a nose ring. Accessories can be represent ed in a
SET
colum n, because a cust om er m ay wish t o select m ore t han one of t hem . I n addit ion, you know from past experience t hat m ost cust om ers
order horns and a cow bell, so it s reasonable t o use t hose for t he colum ns default value.
•
Cust om e r na m e a nd a ddr e ss st r e e t , cit y, st a t e .
The possible st at e nam es are already st ored in t he
states
t able, so t hey can be used as t he basis for t he corresponding form elem ent .
Given t he preceding discussion, a
cow_order
t able m ight be designed like t his: CREATE TABLE cow_order
id INT UNSIGNED NOT NULL AUTO_INCREMENT, cow color, figurine size, and accessory items
color CHAR20, size ENUMsmall,medium,large DEFAULT medium,
accessories SETcow bell,horns,nose ring,tail ribbon DEFAULT cow bell,horns,
customer name, street, city, and state abbreviation cust_name CHAR40,
cust_street CHAR40, cust_city CHAR40,
cust_state CHAR2, PRIMARY KEY id
;
The
id
colum n provides a unique ident ifier for each record. I t s a good idea t o have such a value, and in fact will be necessary when we get t o
Recipe 18.5 , which shows how t o use web
form s t o edit exist ing records. For t hat t ype of act ivit y, you m ust be able t o t ell which record t o updat e, which is difficult wit hout a unique record ident ifier.
The list of available colors is m aint ained in a separat e t able,
cow_color
: CREATE TABLE cow_color
color CHAR20 ;
For purposes of illust rat ion, assum e t hat t he color t able looks like t his: +---------------+
| color | +---------------+
| Black | | Black White |
| Brown | | Cream |
| Red | | Red White |
| See-Through | +---------------+
An applicat ion can use t hese t ables t o generat e list elem ent s in an order ent ry form , m aking it unnecessary for t he applicat ion t o have a lot of specialized built - in knowledge about t he
available opt ions. The next several sect ions describe how t o do t his, and how t o process t he input t hat you obt ain when a user subm it s a form .
18.2 Creating Forms in Scripts