PHP Basics 1-1
PHP Basics 1-1
Basics of PHP - 4
Basics of PHP - 4
1. Updating existing record in MySQL database
Basically, you will need to do the following things.
(1) Show the data from database and let user choose a record to update ( ) Let user modify existing data This requires settin !efault values to HTM" form (!) Update the data in database using SQL UP"#$% command
1.1 "atabase str&ct&re and &pdate operation f the database consists of non!relational "independent# flat tables only, then updating can be done by simply deleting old data and inserting new data into the table.
'o. $ 0 + 1 , 'a(e %ic&ey %ouse %innie %ouse 6onald 6uc& Buffy 6oggy 6umbo 5lephant Sex % 3 % % % Birt)day $'($!)*!)$ $'2*!)'!0+ $'2'!$$!0, 0))0!)*!)+ 0))0!)*!)1 #ddress +,+,, -all street, ./ 4/356, 7a&arta 859:., Surabaya 7 ./, 9o&yo %;5, Somewhere $e*ep)one )0!$0+1!)'2* )0!0+1$!',*+ )0!)1'0!'*$+ )+!(*+$!$0+$ )1!120*!+*$'
1. Delete record
%innie %ouse 0
$'2*!)'!0+
4/356, 7a&arta, 0
)0!0+1$!',*+
<owe=er, if the database has relational tables, updating means %odifying data while &eeping the internal position of record in the table. So it cannot be done by simply deleting > adding, especially when the table has #rimary $ey. Suppose we do update of the following simple "but related# tables participants table
pt+id $ 0 ! 1 , participant+na(e %ic&ey %ouse %innie %ouse "ona*d "&cBuffy 6oggy 6umbo 5lephant pro,ince+id 0 $ ! $ , p,+id $ 0 ! 1 ,
pro,ince table
pro,ince+na(e 7a&arta Surabaya Medan %a&assar Bali
Updating "ona*d "&c- record in participants table is rather easier ?ou can delete the record, and add new record with updated information
But updating Medan record in pro,ince table cannot be done by delete > add, because doing so will change the id of Medan to other =alue. ?ou should rather use SQL UP"#$% command to update the record.
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
1. .nterface for adding or &pdating tab*e t)at )as t)e *in- to ot)er tab*e f the table has a field that is actually a &ey number to another table "li&e participants table#, then that field should be a drop!down list of selectable =alues li&e shown below.
Province field should be a dropdown list that has been generated from province table
n order to display this &ind of drop!down list, it is a good idea to write general!purpose function to produce the list.
host, user, password of %ySQL name of database and table field to be the internal =alue and item currently selected item
Ieneral!purpose function
show_table_droplist
@select nameABpro=inceCidBD @option =alueA$D7a&arta@EoptionD @option =alueA0DSurabaya@EoptionD @option =alueA+ se*ectedD%edan@EoptionD @option =alueA1D%a&assar@EoptionD ... @EselectD
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
show_table_droplist.php 1 @Jphp 2 function showCtableCdroplist " " FhostCname, FuserCname, Fpassword, FdatabaseCname, # FtableCname, FidCname, FfieldCname, FselectedCid# $K % EL .onnect to %ySQL database LE & Flin& A mysMlCconnect"FhostCname, FuserCname, Fpassword# or dieN ' mysMlCselectCdb"FdatabaseCname# or dieN ( 1) EL 5xecute SQL command LE 11 FsMl A BS5L5.9 OFidCnameO, OFfieldCnameO 38;% OFtableCnameOBN 12 Fresult A mysMlCMuery"FsMl#N 1" 1# EL 6isplay drop!down list items LE 1$ while "Fline A mysMlCfetchCarray"Fresult, %?SQLC/SS;.## K 1% echo B@option =alueAPFlineGFidCnameHPBN 1& if "FlineGFidCnameH AA FselectedCid# echo B selectedBN 1' echo BDBN 1( echo htmlspecialchars"FlineGFfieldCnameH#N 2) echo B@EoptionDQnBN 21 R 22 2" EL .losing procedures LE 2# mysMlCfreeCresult"Fresult#N 2$ mysMlCclose"Flin&#N 2% R 2& JD test_droplist.php * +est page for the function 1 @htmlD@bodyD 2 @pD:ro=inceS 3ou can add blan4 item if needed " @select nameABpro=inceCidBD # @optionD@EoptionD ,ame of host- user- password$ @Jphp and database of ./ 01 % reMuireConce"BshowCtableCdroplist.phpB#N & showCtableCdroplist"BlocalhostB,ByoichiB,BB,BtestB, ' Bpro=inceB, Bp=CidB, Bpro=inceCnameB, +#N ( JD ,ame of table to show as drop-down list1) @EselectD 2ield name of the table to be used as value11 @EbodyD@EhtmlD 2ield name of the table to be used as each list item elected item number in drop-down list
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
1.! /,era** genera*-p&rpose database (anip&*ation pages <ere are the sample of general!purpose, o=erall database manipulation pages. ?ou can use these pages by changing database name and E or SQL commands. rdb_main.php rdb_browse.php rdb_delete.php
5dd new
!pdate
Show_MySQL_table2.php show_table_droplist.php
add6update rdb_add_update.php rdb+(ain.p)p 1 2 " # $ @<9%LD@B;6?D @<$D8elational 6atabase %ain :age@E<$D @pD@a hrefABrdbCbrowse.phpBDBrowse 8ecord@EaD@EpD @pD@a hrefABrdbCaddCupdate.phpBD/dd 4ew 8ecord@EaD@EpD @EB;6?D@E<9%LD
rdb+bro0se.p)p 1 @Jphp 2 EL nitialiTe =ariables LE " Fsubmit A BBN EE -hich submit button was clic&ed by user # Fsearch A BBN EE Search string $ Foffset A )N EE Starting position of record to show % FnumCrows A $)N EE 4umber of records to show at a time & FmaxCrow A )N EE %aximum row that has been displayed before
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
' Fselected A BBN EE Selected record for update or deletion ( if "isset"FC:;S9GBsubmitBH# >> FC:;S9GBsubmitBH UA B;VB >> FC:;S9GBsubmitBH UA B4;B# K 1) Fsubmit A FC:;S9GBsubmitBHN 11 Fsearch A FC:;S9GBsearchBHN 12 Foffset A FC:;S9GBoffsetBHN 1" FnumCrows A FC:;S9GBnumCrowsBHN 1# FmaxCrow A FC:;S9GBmaxCrowBHN 1$ if "isset"FC:;S9GBselectedBH## Fselected A FC:;S9GBselectedBHN 1% R 1& 1' EL 8edirect to update or delete page LE 1( if "Fselected UA BB# K 2) if "Fsubmit AA BUpdate %ar&ed 8ecordB# 21 Flocation A BrdbCaddCupdate.phpBN 22 elseif "Fsubmit AA B6elete %ar&ed 8ecordB# 2" Flocation A BrdbCdelete.phpBN 2# if "isset"Flocation## K 2$ header"BLocationS B . Flocation . BJidAFselectedB#N 2% exitN 2& R 2' R 2( ") EL %odify =arable deoending on the submit type LE "1 if "Fsubmit AA BShow /ll 8ecordsB# Fsearch A BBN "2 if "Fsubmit AA BSearch 8ecordB# Foffset A )N "" if "Fsubmit AA B4ext :ageB# Foffset A FmaxCrowN "# if "Fsubmit AA B:re=ious :ageB# K "$ Foffset A Foffset ! FnumCrowsN "% if "Foffset @ )# Foffset A )N "& R "' JD "( #) @htmlD@bodyD #1 @U!! 9hese are for self!learning information !!D #2 @pD@smallD@bD6ebug information for 6e=eloper@EbD@brD #" FC:;S9GPsubmitPH A @Jphp echo FsubmitN JD@brD ## FC:;S9GPsearchPH A @Jphp echo FsearchN JD@brD #$ FC:;S9GPoffsetPH A @Jphp echo FoffsetN JD@brD #% FC:;S9GPnumCrowsPH A @Jphp echo FnumCrowsN JD@brD #& FC:;S9GPselectedPH A @Jphp echo FselectedN JD@brD #' @EsmallD@EpD #( @<$DBrowse 8elational 6atabase@E<$D $)
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
$1 @U!! 6isplay search capability !!D $2 @3;8% methodAB:;S9BD $" @ 4:U9 typeABsubmitB nameABsubmitB =alueABShow /ll 8ecordsBD or $# @ 4:U9 typeABtextB nameABsearchB =alueAB@Jphp echo FsearchN JDBD >nbspN $$ @ 4:U9 typeABsubmitB nameABsubmitB =alueABSearch 8ecordBD $% $& @<8D $' $( @Jphp %) EL 6isplay data table LE %1 reMuireConce"BshowC%ySQLCtable0.phpB#N %2 FsMl A BS%L%1$ pt+id2 participant+na(e2 pro,ince+na(e 34/M participants 2 pro,ince 5H%4% pro,ince+id 6 p,+idBN %" if "Fsearch UA BB# FsMl .A B #'" participant+na(e L.7% 89:searc)98BN %# FsMl .A B /4"%4 B; pt+idBN %$ FnumCfound A showC%ySQLCtable0"B*oca*)ostB, Byoic)iB, BB, BtestB, FsMl, Foffset, FnumCrows, Bpt+idB#N %% %& EL 6isplay number of data and current range LE %' FminCrow A Foffset W $N %( FmaxCrow A Foffset W FnumCrowsN &) if "FmaxCrow D FnumCfound# FmaxCrow A FnumCfoundN &1 echo B@pD@bDFnumCfound@EbD records found E Showing record 4o. @bDFminCrow ! FmaxCrow@EbD@EpDQnBN &2 JD &" &# @U!! 6isplay selection for number of records in a screen !!D &$ @pDShow &% @select nameABnumCrowsBD && @Jphp &' for "FiA$)NFi@A$))NFiWA$)# K &( echo B@option BN ') if "Fi AA FnumCrows# echo B selectedBN '1 echo BDFi@EoptionDBN '2 R '" JD '# @EselectD '$ records at a time '% '& @U!! 6isplay pre=ious E next page button !!D '' @Jphp if "Foffset D $# echo B@ 4:U9 typeAPsubmitP nameAPsubmitP =alueAP:re=ious :agePDBN JD>nbspN '( @Jphp if "Foffset W FnumCrows ! $ @ FnumCfound# echo B@ 4:U9 typeAPsubmitP nameAPsubmitP =alueAP4ext :agePDBN JD
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
() (1 @U!! <idden field to hold current =ariables !!D (2 @ 4:U9 typeABhiddenB nameABoffsetB =alueAB@Jphp echo FoffsetN JDBD (" @ 4:U9 typeABhiddenB nameABmaxCrowB =alueAB@Jphp echo FmaxCrowN JDBD (# ($ @<8D (% (& @U!! 6isplay button for update or delete !!D (' @pD@bD;peration@EbDS %ar& a record, then@EpD (( @ 4:U9 typeABsubmitB nameABsubmitB =alueABUpdate %ar&ed 8ecordBD >nbspN 1)) @ 4:U9 typeABsubmitB nameABsubmitB =alueAB6elete %ar&ed 8ecordBD 1)1 @E3;8%D 1)2 @a hrefABrdbCmain.phpBD8eturn to %ain %enu@EaD 1)" @EbodyD@EhtmlD rdb+add+&pdate.p)p 1 @Jphp 2 reMuireConce"BshowC%ySQLCtable0.phpB#N " # EL nitialiTe all =ariables LE $ FisCupdate A isset"FCI59GBidBH#N EE 98U5 if it is update screen % Fpt+n( A BBN EE :articipant name & Fp,+id A )N EE :ro=ince id ' Ferror A BBN EE 5rror message ( Fsuccess A BBN EE Success message 1) if "FisCupdate# K 11 Ftitle A BUpdate 8ecordBN 12 FbuttonCcaption A BUpdateBN 1" R else K 1# Ftitle A B/dd 4ew 8ecordBN 1$ FbuttonCcaption A B/ddBN 1% R 1& 1' EL Iet current record datas LE 1( if "isset"FC:;S9GBsubmitBH## K 2) FptCnm A FC:;S9GBna(eBHN 21 Fp=Cid A FC:;S9GBpro,ince+idBHN 22 R elseif "FisCupdate# K 2" Flin& A mysMlCconnect"B*oca*)ostB, Byoic)iB, BB# or dieN 2# mysMlCselectCdb"BtestB# or dieN 2$ Fresult A mysMlCMuery"BS%L%1$ < 34/M participants 5H%4% pt+id 6= . FCI59GBidBH#N 2% if "mysMlCnumCrows"Fresult# AA )# dieN
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
2& Fline A mysMlCfetchCarray"Fresult, %?SQLC/SS;.#N 2' FptCnm A FlineGBparticipant+na(eBHN 2( Fp=Cid A FlineGBpro,ince+idBHN ") mysMlCfreeCresult"Fresult#N mysMlCclose"Flin&#N "1 R "2 "" EL :erform SQL command LE "# if "isset"FC:;S9GBsubmitBH## K "$ EL .hec& =alues LE "% if ":pt+n( AA BB# Ferror .A B:lease enter the name of participantBN "& if ":p,+id AA BB# Ferror .A B:lease choose pro=inceBN "' EL f therePs no error, then perform SQL command LE "( if "Ferror AA BB# K #) if "FisCupdate# K #1 FsMl A BUP"#$% participants S%$ participant+na(e68:pt+n(82 pro,ince+id6:p,+id 5H%4% pt+id6B . FCI59GBidBHN #2 Fsuccess A B9he record has been updated.BN #" R else K ## FsMl A B.'S%4$ .'$/ participants >#LU%S (882 8:pt+n(82 :p,+id)BN #$ Fsuccess A B/ new record has been added.BN #% FptCnm A BBN Fp=Cid A )N EE .lear all fieldU #& R #' showC%ySQLCtable0"B*oca*)ostB, Byoic)iB, BB, BtestB, FsMl, BB, BB, BB#N #( R $) R $1 JD $2 @htmlD@bodyD $" @<$D@Jphp echo FtitleN JD@E<$D $# @3;8% methodAP:;S9PD $$ ?p@Participant 'a(eA $% ?.'PU$ type68text8 na(e68na(e8 siBe6!C (ax*engt)6DC ,a*&e6=?Ep)p ec)o :pt+n(F E@=@?Gp@ $& ?p@Pro,inceA $' ?se*ect na(e6=pro,ince+id=@ $( @Jphp %) if "UFisCupdate# echo B@optionD@EoptionDBN %1 reMuireConce"BshowCtableCdroplist.phpB#N %2 showCtableCdroplist"B*oca*)ostB,Byoic)iB,BB,BtestB,Bpro,inceB, Bp,+idB, Bpro,ince+na(eB, :p,+id#N %" JD %# @EselectD@EpD %$ @input typeABsubmitB nameABsubmitB =alueAB@Jphp echo FbuttonCcaption JDBD>nbspN 8
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
%% @Jphp if "UFisCupdate >> Uisset"FC:;S9GBsubmitBH## echo P@input typeABresetB nameABsubmitB =alueAB.learBDPN JD %& @<8D %' @pD@font colorAPredPD@Jphp echo FerrorN JD@EfontD@EpD %( @Jphp &) if "Fsuccess UA BB# echo B@pDFsuccess@EpDBN &1 if "FisCupdate# echo P@a hrefABrdbCbrowse.phpBD8eturn to 6ata Browser@EaDPN &2 if "UFisCupdate# echo P@a hrefABrdbCmain.phpBD8eturn to %ain %enu@EaDPN &" JD &# @E3;8%D &$ @EbodyD@EhtmlD rdb+de*ete.p)p &% @Jphp && EL 8edirect to data browser page LE &' if "Uisset"FCI59GBidBH# XX isset"FC:;S9GBsubmitBH# >> FC:;S9GBsubmitBH AA B4;B# K &( header"BLocationS rdbCbrowse.phpB#N EE 8edirect to browser page ') exitN '1 R '2 JD '" '# @htmlD@bodyD '$ @<$D6elete confirmation@E<$D '% @Jphp '& reMuireConce"BshowC%ySQLCtable0.phpB#N '' if "isset"FC:;S9GBsubmitBH# >> FC:;S9GBsubmitBH AA B?5SB# K '( EL :erform deletion LE () FsMl A B"%L%$% 34/M participants 5H%4% pt+id 6 = . :+P/S$H8id8IF (1 showC%ySQLCtable0"B*oca*)ostB, Byoic)iB, BB, BtestB, FsMl, !$, ), BB#N (2 JD (" @pD@font colorABredBD;ne record has been deleted.@EfontD@EpD (# @a hrefABrdbCbrowse.phpBD8eturn to 6ata Browser@EaD ($ @Jphp (% R else K (& EL Show record to be deleted once again LE (' FsMl A BS%L%1$ pt+id2 participant+na(e2 pro,ince+na(e 34/M participants 2 pro,ince 5H%4% pro,ince+id 6 p,+id #'" pt+id 6 = . FCI59GPidPHN (( FnumCfound A showC%ySQLCtable0"B*oca*)ostB, Byoic)iB, BB, BtestB, FsMl, !$, ), BB#N 1)) if "FnumCfound AA )# Kecho "B4o record foundB#N dieNR 1)1 JD 9
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
@U!! Show confirmation message and ?5SE4; button !!D @pD@font colorABredBD/re you sure to delete this recordJ@EfontD@EpD @3;8% methodAB:;S9BD @ 4:U9 typeABhiddenB nameABidB =alueAB@Jphp echo "FCI59GBidBH#N JDBD>nbspN 1)% @ 4:U9 typeABsubmitB nameABsubmitB =alueAB?5SBD>nbspN 1)& @ 4:U9 typeABsubmitB nameABsubmitB =alueAB4;BD 1)' @E3;8%D 1)( @Jphp 11) R 111 JD 112 @EbodyD@EhtmlD 1)2 1)" 1)# 1)$ s)o0+MySQL+tab*e .p)p 1 @Jphp 2 function showC%ySQLCtable0 " " FhostCname, FuserCname, Fpassword, FdatabaseCname, # FsMl, Foffset, FnumCrows, FidCfield# $K % EL .onnect to %ySQL database LE & Flin& A mysMlCconnect"FhostCname, FuserCname, Fpassword# or dieN ' mysMlCselectCdb"FdatabaseCname# or dieN ( 1) EL 5xecute SQL command LE 11 Fresult A mysMlCMuery"FsMl#N 12 if "UFresult# K 1" echo B@BD"5rror in SQL#@EBD B . mysMlCerror"#N 1# dieN 1$ R 1% 1& EL f the SQL command is not S5L5.9, then stop function here LE 1' if "strtoupper"substr"FsMl,),(## UA BS5L5.9B# K 1( mysMlCclose"Flin&#N EE .losing connection 2) returnN 21 R 22 2" EL Iet number of records found LE 2# FnumCfound A mysMlCnumCrows"Fresult#N 2$ 2% if "FnumCfound AA ) # K 2& mysMlCclose"Flin&#N EE .losing connection 2' return )N 2( R ")
10
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
"1 EL ;utput results as <9%L table LE "2 echo B@table borderDQnBN "" EL ;utput field names as table header LE "# echo B@trDQnBN "$ if "FidCfield UA BB# echo B@th bg.olorAPredPDmar&@EthDBN "% for "FfieldA)N Ffield@mysMlCnumCfields"Fresult#N FfieldWW# K "& echo B@thDB . mysMlCfieldCname"Fresult, Ffield# . B@EthDQnBN "' R "( echo B@EtrDQnBN #) #1 EL ;utput all records LE #2 for "Frow A )N Frow @ mysMlCnumCrows"Fresult#N FrowWW# K #" Fline A mysMlCfetchCarray"Fresult, %?SQLC/SS;.#N ## if "Foffset @) XX Frow DA Foffset >> Frow @ Foffset W FnumCrows# K #$ echo B@trDQnBN #% if "FidCfield UA BB# echo B@td alignAPcenterPD@input typeAPradioP nameAPselectedP =alueAPB. FlineGFidCfieldH . BPD@EtdDBN #& foreach "Fline as Fcell# K #' if "isCnumeric"Fcell## K #( echo B@td alignAPrightPDBN $) echo numberCformat"Fcell#N $1 echo B@EtdDQnBN $2 R else K $" echo B@tdDFcell@EtdDQnBN $# R $$ R $% echo B@EtrDQnBN $& R $' R $( echo B@EtableDQnBN %) %1 EL .losing procedures LE %2 mysMlCfreeCresult"Fresult#N EE 3ree result %" mysMlCclose"Flin&#N EE .losing connection %# return FnumCfoundN %$ R %% JD
11