Vous êtes sur la page 1sur 12

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

Basics of PHP - 3
1. Adding new record to MySQL database
Basically, you will need to do the following things.

(1) Get new values from the user using HTML form. (2) Validate new data (This is the most important part) (3) dd new record to data!ase using "#L $ !S"#$% command
("%a&'(e) "u&&ose we have the following ta!le of &artici&ants.
!o. ' 2 . 3 / !a&e Mic(ey Mouse Minnie Mouse 8onald 8uc( Buffy 8oggy 8um!o 7le&hant Se% M 5 M M M Birt)day ')*'+,-+,' ')4-+,)+2. ')4)+''+2/ 2,,2+,-+,. 2,,2+,-+,3 Address ././, 0all street, 1 6 578, 9a(arta :7T;1, "ura!aya 9<1 , To(yo M=7, "omewhere $e(e')one ,2+'2.3+,)4,2+2.3'+)/-. ,2+,3)2+)-'. ,.+*-.'+'2.' ,3+342-+.-')

1.1 Modify s)ow*MySQL*tab(e f+nction <n order to correctly handle $<6"7:T% command or other "#L commands than $"7L71T%, our general+&ur&ose function show>My"#L>ta!le must !e modified as follows. s)ow*MySQL*tab(e.')' ... ?@ 7Aecute "#L command @? Bresult C mysDl>Duery(BsDl)E if (FBresult) G echo HIBJ(7rror in "#L)I?BJ H . mysDl>error()E dieE K ?@ <f "#L command is not "7L71T, then eAit function here @? if (strtou&&er(su!str(BsDl,,,*)) FC H"7L71TH) G mysDl>close(Blin()E Exit from function returnE K ?@ =ut&ut results as HTML ta!le @? echo HIta!le !orderJnHE ...

Add these lines

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

1.2 H$ML ,or& for t)e data entry MP-#$A!$L Basic data validation can !e done easily !y HTML form itself (without ;H;).

+ate should !e entered ! $re" defined com!o"!ox rather than text !ox, !ecause *e can force user to enter onl the )alid num!er for ear, month and da

(ele$hone num!er should also !e entered li-e this *a , so that *e can )alidate the )alue easil add*record.')' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 IHTMLJIB=8MJ IH'J dd new record to &artici&ants data!aseI?H'J I5=:M methodCH;="THJ I&J6ameLI<6;NT ty&eCHteAtH nameCHnameH siOeCH/,H maAlengthCH/,HJI?&J I&J"eAL I<6;NT ty&eCHradioH nameCHseAH valueCHMHJMale I<6;NT ty&eCHradioH nameCHseAH valueCH5HJ5emale I?&J I&JBirthdayL size and maxlength should !e used to limit Mear the maximum si&e of the data' (hese )alues must match *ith the definition of each field in data!ase Iselect nameCHyearHJ HIo&tionJI?o&tionJH IP&h& for (By C '),,E By IC date(HMH)E ByQQ) G echo HIo&tionJByI?o&tionJnHE K PJ I?selectJ Month Iselect nameCHmonthHJ HIo&tionJI?o&tionJH IP&h& for (Bm C 'E Bm IC '2E BmQQ) G echo HIo&tionJBmI?o&tionJnHE K PJ I?selectJ 2

PHP code to automaticall add items to com!o"!ox # ear from 1900 u$ to this ear% PHP code to automaticall add items to com!o"!ox #month from 1 to 12%

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

8ay Iselect nameCHdayHJ HIo&tionJI?o&tionJH IP&h& for (Bd C 'E Bd IC .'E BdQQ) G echo HIo&tionJBdI?o&tionJnHE K PJ

PHP code to automaticall add items to com!o"!ox #da from 1 to 31%

I?selectJ I?&J I&J ddressLI<6;NT ty&eCHteAtH nameCHaddressH siOeCH',,H maAlengthCH',,HJI?&J I&JTele&honeL I<6;NT ty&eCHteAtH nameCHtel'H siOeCH3H maAlengthCH3HJ+ I<6;NT ty&eCHteAtH nameCHtel2H siOeCH3H maAlengthCH3HJ+ I<6;NT ty&eCHteAtH nameCHtel.H siOeCH3H maAlengthCH3HJ I?&J I&JI<6;NT ty&eCHsu!mitH nameCHsu!mitH valueCH dd :ecordHJI?&J I&JI<6;NT ty&eCHresetH nameCHresetH valueCH1learHJI?&J I?5=:MJ IH:J

1.3 PHP initia(i.ation add*record.')' (continued) 50 51 52 53 54 55 56 57 IP&h& function invalid(Berror>message) G echo HI&JIfont colorCRredRJBerror>messageI?fontJI?&JHE echo HI?B=8MJI?HTMLJHE dieE .ame as $re)ious $ractice K reDuire>once(Hshow>My"#L>ta!le.&h&H)E

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

1./ 0a(idation of entered data add*record.')' (continued) 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 ?@ <f it is the initial screen, then sto& here @? if (Fisset(B>;="TSHsu!mitHT)) invalid(HH)E

MP-#$A!$0 (his code is needed for the first )isit to the $a1e' 2ithout this code, it *ill dis$la error #!ecause no data has !een sent%

?@ 1hec( whether all values are &osted or not @? Berror C HHE isset must !e used if (B>;="TSHnameHT CC HH) Berror .C H7nter 6ameIB:JHE for option button or checkbox !ecause if (Fisset(B>;="TSHseAHT)) Berror .C H7nter "eAIB:JHE key )alue *ill not !e if (B>;="TSHyearHT CC HH exist if user does not UU B>;="TSHmonthHT CC HH select the o$tion or UU B>;="TSHdayHT CC HH) Berror .C H7nter BirthdayIB:JHE chec-!ox if (B>;="TSHaddressHT CC HH) Berror .C H7nter ddressIB:JHE if (B>;="TSHtel'HT CC HH means 3456 in PHP' 7or 3A8+6, use && UU B>;="TSHtel2HT CC HH UU B>;="TSHtel.HT CC HH) Berror .C H7nter Tele&honeIB:JHE if (Berror FC HH) invalid(Berror)E /hec- )alid date ! function ?@ 1hec( Birthday @? if (Fchec(date(B>;="TSHmonthHT,B>;="TSHdayHT,B>;="TSHyearHT)) invalid(H<nvalid !irthdayH)E ?@ 1hec( Tele&hone @? /hec- if all data is numeric if (Fis>numeric(B>;="TSHtel'HT) UU Fis>numeric(B>;="TSHtel2HT) UU Fis>numeric(B>;="TSHtel.HT)) invalid(HTele&hone is not num!erH)E

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

1.1 Addition of new data to MySQL database add*record.')' (The end of &age) 84 85 86 87 88 89 90 91 92 93 94 95 96 97 ?@ 5inally, we can add record @? BsDl C H<6"7:T <6T= &artici&ants V LN7" H . s&rintf(R(HH,HVsH,HVsH,HVs+Vs+VsH,HVsH,HVs+Vs+VsH)R, B>;="TSHnameHT, B>;="TSHseAHT, B>;="TSHyearHT, B>;="TSHmonthHT, B>;="TSHdayHT, B>;="TSHaddressHT, B>;="TSHtel'HT, B>;="TSHtel2HT, B>;="TSHtel.HT)E echo HI&JH, htmls&ecialchars(BsDl), HI?&JHE show>My"#L>ta!le(HlocalhostH,HyoichiH,HH,HtestH,BsDl)E echo HI&JI!J6ew record has !een successfully addedI?!JI?&JHE PJ I?B=8MJI?HTMLJ

sprintf is a )er useful function that returns a strin1 that com!ines the )alues of one or more )aria!les usin1 s$ecified format

1.2 &'ro3e&ent consideration Though this code wor(s well, we should consider many necessary im&rovements when you will develo& real 0e! site. 5ollowings are some of the im&ortant im&rovements. Pre3ent d+'(ication of data W <f the data!ase should not have du&licated data, you should first search for the same data in data!ase so that there will !e no same data in the data!ase. <n order to im&lement this function, some additional modification is needed for our general+&ur&ose function show>My"#L>ta!le ( see 2 2) Better retry interface for error W 0hen user made even tiny mista(e, all in&ut data will !e disa&&eared and must !e entered from the !eginning. The form should !e a!le to dis&lay the user+entered data as the initial value in each form element. Below is the eAam&le of inserting initial value for in&ut+teAt !oA. I&J6ameLI<6;NT ty&eCHteAtH nameCHnameH siOeCH/,H maAlengthCH/,H 3a(+e4567')' ec)o )t&(s'ecia(c)ars(8*P-S$9:na&e:;) 7<5JI?&J

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

2. =e(eting a record fro& MySQL database


Basically, you will need to do the following things. (1) "how the data from data!ase and let user choose a record to delete (2) s( the user for confirmation of the deletion (3) 8elete the data from data!ase using "#L $="L"$"% command ("%a&'(e) 0e use the same eAam&le as '. 2.1 nterface design for t)e record de(etion The interface design for record deletion is very critical. 0e can thin( of many &ossi!le interfaces, !ut we should !e very careful to choose the !est interface !ased on the user+ friendliness as well as the easiness of develo&ment. nterface design for t)e de(etion 1

Ad3antageL Very easy to develo&. Mou can use almost the same code as $record addition% in &ractice ', only changing the "#L command from $<6"7:T X% to $87L7T7 X%. =isad3antageL There is no dis&lay of eAisting record to !e deleted. <t is very difficult to use. Nser must enter eAactly the same data to delete.

nterface design for t)e de(etion 2

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

Ad3antageL 7asy to use !ecause user can see the list of eAisting records. <t is also convenient if the user needs to delete multi&le records at once. =isad3antageL 6ot so easy to write ;H; code to &erform this function. <t is also necessary to im&lement $multi&le &age dis&lay% if num!er of records are very large. nterface design for t)e de(etion 3

1' 7irst, user searches the record to delete

2' (hen, user deletes the data that found

Ad3antageL :ather easy to develo&. The same interface can !e used for $"earch% &age. <t is also useful for the data!ase with large num!er of records. Multi&le records deletion is also &ossi!le de&ending on the search result. =isad3antageL Two+ste& o&eration is needed. <t is not so easy to use if the user wants to delete only ' record !ut there are so many $similar% records (Getting search result with only ' match might !e difficult)

<n this &ractice, we ado&t design 3.

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

2.2 Modify s)ow*MySQL*tab(e f+nction 0e need the information of num!er of records found !y $"7L71T% command, so our general+&ur&ose function show>My"#L>ta!le must !e modified as follows. s)ow*MySQL*tab(e.')' ... ?@ <f the "#L command is not "7L71T, then sto& function here @? if (strtou&&er(su!str(BsDl,,,*)) FC H"7L71TH) G mysDl>close(Blin()E ?? 1losing connection returnE K ?@ Get num!er of records found @? Bnum>found C &ys>(*n+&*rows(Bresult)E echo HI&J(Bnum>found records found)I?&JnHE if (Bnum>found FC ,) G ?@ =ut&ut results as HTML ta!le @? echo HIta!le !orderJnHE ... echo HI?ta!leJnHE K ?@ 1losing &rocedures @? mysDl>free>result(Bresult)E ?? 5ree result mysDl>close(Blin()E ?? 1losing connection return Bnum>foundE K PJ Add this line #/losin1 !race for if statement%

Add these lines

9et num!er of records

4ut$ut num!er of records on screen 4ut$ut the ta!le onl if one or more records found

Add this line #5eturn num!er of records found%

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

2.3 H$ML ,or& for data searc) HTML form is almost the same as &ractice '. Mou only change the message teAt as follows. de(ete*record.')' IHTMLJIB=8MJ 6H1<Searc) ?a&'@ =e(ete record fro& 'artici'ants database6AH1< 6H3<"nter one or &ore fie(ds to searc)6AH3< I5=:M methodCH;="THJ ... 3/ I&JI<6;NT ty&eCHsu!mitH nameCHsu!mitH valueCHSearc) #ecordHJI?&J 3* I&JI<6;NT ty&eCHresetH nameCHresetH valueCH1learHJI?&J 3- I?5=:MJ 2./ PHP initia(i.ation <t is almost the same as &ractice ' eAce&t that we should change the name of function in3a(id. de(ete*record.')' (continued) 34 IP&h& 3) function sto'(Berror>message) G ... /* reDuire>once(Hshow>My"#L>ta!le.&h&H)E 2.1 -3era(( s+b&it 3a(idation 5irst of all, we chec( if the &age has !een shown for the first time (without &osting of any data), or user chose $!-% for the confirmation of deletion. <n !oth case, we sto& the ;H; code here. /4 ?@ <f it is the initial screen or user has clic(ed H6=H !utton @? /) if (Bisset(8*P-S$95s+b&it5;) CC 8*P-S$95s+b&it5;445!-5) sto'(55)@ *, MP-#$A!$L <n this eAam&le, all . su!mit !uttons on the &age ($Searc) #ecord%, $D"S%, and $!-%) have the same na&e attri!ute as $s+b&it% (i.e. nameC%su!mit%). Therefore, the &osted value from the form 8*P-S$95s+b&it5; can also ta(e . different values ($Searc) #ecord%, $D"S%, or $!-%) de&ending on which !utton the user clic(ed. :t is !etter to chan1e the name of function invalid to stop !ecause *e use this function not onl for error, !ut also for normal termination ' 2 . 3

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

2.2 =e(etion of data if +ser confir&ed it 6eAt, we will chec( if the user has clic(ed $D"S% !utton for confirmation of deletion. <f that case, we will actually delete the record. *' ?@ <f user has clic(ed HM7"H !utton @? *2 if (B>;="TSHsu!mitHT CC HM7"H) G *. BsDlCH87L7T7 5:=M &artici&ants 0H7:7H.stri&slashes(B>;="TSHwhereHT)E *3 show>My"#L>ta!le(HlocalhostH, HyoichiH, HH, HtestH, BsDl)E */ sto&(H"&ecified record has !een deleted.H)E Execution sto$s here ** K MP-#$A!$L This code will not !e eAecuted at all when the user visited the &age for the first time, or when the user searches for the record. <t will !e eAecuted only when the user clic(ed $D"S% !utton. stri's(as)es function removes unnecessary !ac(slashes ($ ) in &osted string (which 0e! !rowser adds automatically) 8*P-S$95w)ere5; is the 0H7:7 &hrase for "#L command to !e used for the deletion. This value will !e created only after the user searches any record ( see 2.-X2.4). 2.E Freating GH"#" ')rase for t)e searc) 6ow, we &re&are GH"#" &hrase that will !e used for S"L"F$ command for the search as well as ="L"$" command later. The secret here is that we must use the same 0H7:7 &hrase for !oth "7L71T and 87L7T7 command, so that the user can delete eAactly the same record it finds. **4 *) -, -' -2 -. -3 -/ -* --4 -) 4, 4' 42 4. 43 4/ 4* ?@ ssign short varia!les to each field for easy coding @? Bnm C B>;="TSHnameHTE if (isset(B>;="TSHseAHT)) BsA C B>;="TSHseAHTE else BsA C HHE B!d C B>;="TSHyearHT.H+H.B>;="TSHmonthHT.H+H.B>;="TSHdayHTE Bad C B>;="TSHaddressHTE Btl C B>;="TSHtel'HT.H+H.B>;="TSHtel2HT.H+H.B>;="TSHtel.HTE ?@ 1reate 0H7:7 &hrase for the search and delete @? Bwhere C HHE if (Bnm FC HH) Bwhere .C H name L<Y7 RVBnmVRHE if (BsA FC HH) Bwhere .C H 68 seACRBsARHE if (B!d FC H++H) Bwhere .C H 68 !irthdayCRB!dRHE if (Bad FC HH) Bwhere .C H 68 address L<Y7 RVBadVRHE if (Btl FC H++H) Bwhere .C H 68 tele&hone L<Y7 RVBtlVRHE if (su!str(Bwhere,',.) CC H 68H) Bwhere C su!str(Bwhere, 3)E ?@ <f there was no data entered, then we reZect the reDuest @? if (Bwhere CC HH) sto&(HMou must enter at least one field to search.H)E

10

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

2.H Perfor& searc) and dis'(ay confir&ation b+ttons for de(etion 5inally, we &erform record search !y using "#L $S"L"F$% command. nd if there is any records found, we dis&lay confirmation !uttons ($D"S% and $!-%) so that the user can confirm the deletion. 4- ?@ ;erform search for the s&ecified records @? 44 BsDl C H"7L71T @ 5:=M &artici&ants 0H7:7H . BwhereE 4) Bnum>found C show>My"#L>ta!le(HlocalhostH,HyoichiH,HH,HtestH,BsDl)E ), )' ?@ "how delete confirmation !utton if record found @? )2 if (Bnum>found J ,) G ). echo H6,-#M &et)od4RP-S$R<HE )3 echo R6 !PI$ ty'e4H)iddenH na&e4Hw)ereH 3a(+e4HR . )/ )t&(s'ecia(c)ars(8w)ere) . RHRE )* echo HI&JIfont colorCRredRJHE )echo H re you sure to delete Bnum>found recordP[n!s&EHE )4 echo H6 !PI$ ty'e4:s+b&it: na&e4:s+b&it: 3a(+e4:D"S:<[n!s&EHE )) echo H6 !PI$ ty'e4:s+b&it: na&e4:s+b&it: 3a(+e4:!-:JHE ',, echo HI?fontJI?&JHE ',' echo H6A,-#M<HE ',2K ',.PJ ',3I?B=8MJI?HTMLJ

11

ADMTC-UCSC-University of Colombo

Basics of PHP - 3

MP-#$A!$L 1onfirmation !uttons ($D"S% and $!-%) are located in anot)er H$ML for&. Mou can include multi&le forms within one &age, so that you can &ost different information !y different forms. <n line )3X)/, we use single Duotation (:) instead of dou!le Duotation (H) to enclose echo statement. This is !ecause the varia!le 8w)ere already contains single Duotation, and that we must ma(e all the strings valid in terms of Duotation usage. <n other words, any enclosing sym!ols in ;H; must !e used as &air with no conflict with others. !"#ample$
1st-level #'% 3rd-level #"% 3rd-level #;% $where

echo '<INPUT type="hidden" value="name LIKE '%kogu e%'"!'


4th-level #'%

2nd-level #<<>%

Alternate notation !in%entation$ ec)o : 6 !PI$ ty'e 4 5 )idden 5 3a(+e 4 5 na&e L J" : KLog+reK : 5 < :
1st-level #'% 2nd-level #<<>% 3rd-level #"% $where 4th-level #'%

6 !PI$ ty'e45)idden5< is a teAt !oA that will not a&&ear on the screen. Mou can use this hidden teAt !oA to store information that should !e &osted again to 0e! server. <n this case, we store the 0H7:7 &hrase that we will use for record deletion.

12

Vous aimerez peut-être aussi