Tải bản đầy đủ - 0 (trang)
Chapter 18. Creating an Online Address Book

Chapter 18. Creating an Online Address Book

Tải bản đầy đủ - 0trang

PlanningandCreatingtheDatabaseTables

Whenyouthinkofanaddressbook,theobviousfieldscometo

mind:name,address,telephonenumber,emailaddress.

However,ifyoulookatyourownpaper-basedaddressbook,

youmaynotethatyouhaveseveralentriesforoneperson.

Maybethatpersonhasthreetelephonenumbers,ortwoemail

addresses,andsoforth.Inyouronlineaddressbook,asetof

relatedtableswillhelpalleviatetheredundancyandrepetition

ofinformation.

Table18.1showssampletableandfieldnamestouseforyour

onlineaddressbook.Inaminute,you'llcreatetheactualSQL

statements,butfirstyoushouldlookatthisinformationandtry

toseetherelationshipsappear.Askyourselfwhichofthefields

shouldbeprimaryoruniquekeys.

Table18.1.AddressBookTableandFieldNames

TableName



FieldNames



master_name



id,date_added,date_modified,f_name,l_name



address



id,master_id,date_added,date_modified,address,city,state,

zipcode,type



telephone



id,master_id,date_added,date_modified,tel_number,type



fax



id,master_id,date_added,date_modified,fax_number,type



email



id,master_id,date_added,date_modified,email,type



personal_notes id,master_id,date_added,date_modified,note



Noticetheuseofdate-relatedfields;eachtablehasa

date_addedanddate_modifiedfieldinit.Thefieldswillhelp



maintainyourdata;youmayatsomepointwanttoissuea

querythatremovesallrecordsthatareolderthanacertain

numberofmonthsoryears,orthatremovesallrecordsthat

haven'tbeenupdatedwithinacertainperiodoftime.

AsyoucanseeinthefollowingSQLstatements,the

master_nametablehastwofieldsbesidestheIDanddaterelatedfields:f_nameandl_name,forfirstnameandlastname.

Theidfieldistheprimarykey.Nootherkeysneedtobe

primaryorunique,unlessyoureallywanttolimityouraddress

booktooneJohnSmith,oneMaryJones,andsoforth.

Thefieldlengthsforthetextfieldsinthefollowingstatementsare

arbitrary;youcanmakethemaslongorasshortasyouwant,

withintheallowabledefinitionofthefieldtype.



mysql>createtablemaster_name(

->idintnotnullprimarykeyauto_increment,

->date_addeddatetime,

->date_modifieddatetime,

->f_namevarchar(75),

->l_namevarchar(75)



->);

QueryOK,0rowsaffected(0.01sec)

Theaddresstablehasthebasicprimarykeyidfieldandthe

date_addedanddate_modifiedfields.Inaddition,youshould

nowseewheretherelationshipwillbemadethroughtheuseof

themaster_idfield.Themaster_idwillbeequaltotheidfield

inthemaster_nametable,matchingthepersonwhoseaddress

thisis.Themaster_idfieldisnotauniquekeybecauseitisa

perfectlyvalidassumptionthatonepersonmayhaveseveral

addressentries.Weseethisinthetypefield,whichisdefined

asanenumeratedlistcontainingthreeoptions:home,work,or

other.Apersonmayhaveoneormoreofallthreetypes,sono

otherkeysarepresentinthistablebesidestheprimarykeyid.

AssumingthisparticularaddressbookcontainsonlyUnited

Statesaddresses,weroundoutthetablewithaddress,city,

state,andzipcodefields.



mysql>createtableaddress(

->idintnotnullprimarykeyauto_increment,

->master_idintnotnull,

->date_addeddatetime,

->date_modifieddatetime,

->addressvarchar(255),



->cityvarchar(30),

->statechar(2),

->zipcodevarchar(10),

->typeenum('home','work','other')

->);

QueryOK,0rowsaffected(0.01sec)

Thetelephone,fax,andemailtablesareallvariationsonthe

sametheme:



mysql>createtabletelephone(

->idintnotnullprimarykeyauto_increment,

->master_idintnotnull,

->date_addeddatetime,

->date_modifieddatetime,

->tel_numbervarchar(25),

->typeenum('home','work','other')

->);



QueryOK,0rowsaffected(0.01sec)



mysql>createtablefax(

->idintnotnullprimarykeyauto_increment,

->master_idintnotnull,

->date_addeddatetime,

->date_modifieddatetime,

->fax_numbervarchar(25),

->typeenum('home','work','other')

->);

QueryOK,0rowsaffected(0.00sec)



mysql>createtableemail(

->idintnotnullprimarykeyauto_increment,

->master_idintnotnull,

->date_addeddatetime,

->date_modifieddatetime,

->emailvarchar(150),

->typeenum('home','work','other')



->);

QueryOK,0rowsaffected(0.00sec)

Thepersonal_notestablealsofollowsthesamesortof

pattern,exceptthatmaster_idauniquekeyandallowsonly

onenotesrecordperperson:



mysql>createtablepersonal_notes(

->idintnotnullprimarykeyauto_increment,

->master_idintnotnullunique,

->date_addeddatetime,

->date_modifieddatetime,

->notetext

->);

QueryOK,0rowsaffected(0.00sec)

Nowthatyourtablesarecreated,youcanworkthroughthe

formsandscriptsformanagingandviewingyourrecords.



CreatingaMenu

Youronlineaddressbookwillcontainseveralactions,soit

makessensetocreateamenuforyourlinks.Listing18.1

createsamenuforallthescriptsyouwillcreateinthissection,

calledmymenu.php.



Listing18.1AddressBookMenu



1:

2:

3:MyAddressBook

4:

5:

6:

MyAddressBook



7:

8:

Management

9:





13:

14:

Viewing

15:



18:

19:

Figure18.1showstheoutputofListing18.1.You'lltackleeach

oftheseitemsinorder,startingwith"AddanEntry"inthenext

section.



Figure18.1.Addressbookmenu.



CreatingtheRecordAdditionMechanism

Justbecauseyou'llpotentiallybeaddinginformationtosix

differenttablesdoesn'tmeanyourformorscriptwillbe

monstrous.Infact,yourscriptswon'tlookmuchdifferentfrom

anyoftheonesyoucreatedinpreviouslessons,andwith

practice,youwillbeabletomaketheseverbosescriptsmuch

morestreamlinedandefficient.

InListing18.2,youcanseeabasicrecordadditionscript,called

addentry.php.



Listing18.2BasicRecordAdditionScriptCalled

addentry.php



1:
2:if($_POST[op]!="add"){

3://haven'tseentheform,soshowit

4:$display_block="

AddanEntry



5:

6:

First/LastNames:




7:


8:
9:



10:

Address:


11:

12:

13:

City/State/Zip:




14:


15:



16:
17:

18:

AddressType:




19:


20:



21:
22:

23:

TelephoneNumber:




24:


25:


26:



27:
28:



29:

FaxNumber:




30:


31:


32:



33:
34:

35:

EmailAddress:




36:


37:


38:


39:
40:

41:

PersonalNote:




42:
43:

44:



45:


46:";



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 18. Creating an Online Address Book

Tải bản đầy đủ ngay(0 tr)

×