Sunteți pe pagina 1din 4

TSST DRIVS

Send an email to the mailing iist using the Send Email form.
Download the code for the Send Email web page from the Head First l.abs web site m """d thit
at www. headf irstlabs . com/books/hfphp It’s in the chapter03 folder. Similar to the your email addrea
Add Email page you saw carlier. titis code consists of a vveh form in sendemail. w.ll need io be o"
html. a slyle sheet istyle . css). and a couple of images elvislogo. gif and blankf ace. •the ma<lir>5 Iist'«
jpg . order far you to
iretewe a message
Create a new tcxt file called sendemail .php, and enter all of the code on the faring
page. Upload all of these Oles to your web server and open the sendema il. html
page in a web browser. Enter an email message in the form, and rlick Submit.

You've got waiL.frow Elmr\


At last. Elmer can send out his MakeMeElvis.com sale emails to everyone on his
mailing lisi bv using his new Send Email web form and PI1P seript. He can also use
the output from the seript to confirm that each message is successfully being sent.
Each time the code in the seript's while loop executes, lie sees “Email sent to
someone(«:sotnewhere.com" with the email address of the person in bis database.
The end result is more exposurc for his produets, and for better or worse, more Elvis
look-alikes!
Script Send Email benar-benar
mengirimkan emos ke alamat yang
tersimpan dalam database, jadi berhati-
hatilah saat berelasi dengan itu!

'ra' to: jwwtr.rtjjrraiairckpw.in ,„ n


l.tn>d Via to: jfiiM-.m
sarm.iui k f,»n Ijma watiu
1'cii.iil anu <o
255SK5SJV-M--'------------- ls#UtltunBU|>.„„„
.„n, - o.S s'1 l.miit irw m: JIIUWI u t ur.ilun'i , ,,m
I jmil 0,11 lo <t,„rvilhr
Ko.t.l «u
EimJ ini l„: no,T,W:lop„J„ra
KnviJ «T,, |„ rnimMm,Ji)«n4lr.un Enuil «ol ir,
«jtiiu migti innniJ uD nn r.nuj «fM lo: n
hiraiui,-, i om
EnuJ om u,
Elmil «u io l»i»i-%l1ooA,Ji,.|nc,.,TO Enuil o-m k.
pi<0 Imicvaoil. om toikitk-nl m IkiUrhfool.inmBr
„n Em.l «M lo: KoKokM kpo„ ,„m
Enuil „m u. »4n.„,i,«,tK1„,(|„ „TO
""**ont lo
«mj»ir.sii«u.buik,.air.,».,
t.iiul »k u,
l»Oi4^niMlk*.nri (kiwi
wol in:

you are here > 145

www.it-ebooks.info

ourappneeds elere
functionality

Sowetiwes
people want
out
As with i.nv blossonting
new business, there are
bunips in the road. ll
seemssome Elvis lans
havejumped ship on the
King and want ofT
Elmer's mailing Iist.
Elmer wants to oblige.
but that means he needs
to remove the customers
from his database.

Dear Elmer.
I do not wisti to
roce'veany more
sales emailsf« WBv
Store. I'm still a fan
of Elvis but I can no
longer look the pari
Please take me off of
your Iist My email is
cbriggs@boards-r-
us.com.
Thanks.
An Ex-lmpersonator

Dear Sir, sidebums, l've deaded that


ZnTlT!n9'ikeBv,Sisn'™Y
Vea90 dCapebul
Sidl bum°s °
Pleasfr^ ejUS'100 much rifaf® rem°ve me from y0Ur email Iist. 7

Vours Truly,
Brian Powers Elmer’s not too haf?y dbout
bp@honey-doit.com losinj Customers, but he
wants to honor fhenr rejvesb
to be removed from his
mailing Iist-

h’s a laci of MySQL lifc sometitnes you need to remove data from your database. Elmer needs to expand bis
applieation to delete users from theemai 1 Iist lable.

Write down the new applieation componcnts you think Elmer is going to
need to implement the Remove Email fcatnre:

146 Chapter 3

www.it-ebooks.info

create and populate a database

fcewoving data with PELETE


To delete data from a lable. we need a new SQL eommand, DELETE.
^ Creotc a database ondtobfefor
We'll use DELETE in a new Remove Email seript that deletes customers'
the email Iist
data from Elmer's mailing Iist. In lari, we need a new seript and a new
O—Cncate an AM EmaH web form-
web lorm t>> drive it... but first we need DELETE.
-and-PHR-seripT for adding-a new -to
The DELETE SQL eommand rcinoves rows of data from a lable. This the Iist:
makes it a eommand you sliould use very earelitlh since it's capahle of 0—Createa
wiping out a lable liill of data in the hlink of an eye. Knowing this. here's ond PHP-scnpF-for-acndtng
the most dangemus form of DELETE. whieh deletes every row from a email to the Iist. -
table. O Cret
Create
and a Remove Email web form'
and
custPHP seript for removing o
customer from the Iist.
This is the njme of
DELETE FROM table_name the table you want to
7 delete rows -fr om Looks l.ke we need
new stey somet.mes
des.jn ylans thany!
W.thout any other jual'rfiers, the
DELETE Commird tomfletely
emyt.es a table of all^ rU data. So we con neverdelete
anything from a table
without deleting everything’

No, not at all. DELETE can be used to pinpoint a specific row or rows
for deietion.
To precisely target the row or rows you w ani lo delete with DELETE. you need to tark
on a WHERE clause. If yon recall from using it with the SELECT eommand. WHERE
allows you to isolate speeilic rows in a (|uery.

_ v^harpen your
DELETEpencil
r h WHERE
Suppose Elmer had 23 customers with a first name of Anne, 11 customers with a last
Y^Harpen your name of Parker, and one customer with the name Anne Parker. Write down how
pencil many rows of data are deleted by each of these gueries.

flnlntinnIist WHERF, first name = 'Anne';


DELETE FROM v email

DELETE FROM email Iist WHERE first name = 'Anne' OR last name = 'Parker';
A WHERE

DELETE FROM email Iist WHERE last name = Parker; clause narrovs down a f|ueiy
WHERE
Suppose Elmer had 23 customers lo focus on speeilic rows
last ol
T' Te.tkwith a first name
<\uestior.l of Anne,
The last na»e 11
isucustomers
t with a
The name of a name of Parker, and one
Th.s pa*-t customer
of the WHER&
'' ---- withtlause
<\uoted, the
SOname Anne
no rows 3rtParker.
deletedWrite
- all down
— how
DELETE
'table FROM
tolwvinFROM
DELETE email
email Iist
@honey-doit.com'
many
Iist WHERE rows
last
first of
namedata==
name
performs are deleted
aParker;
'Anne';
'Anne'
test on
The by each
ORte*t
every tovaof
www.it-ebooks.info
last
row
value these
name
toues
«atth = be
«u»st <\uotedyou are here ► 11 147
gueries. dai
'Parker'; a.
Use WHERE to PEIETE specific data
I5y using n WHERE clause with the DELETE eommand, we target speeilic rows of data for
deietion, instead of emptying an entire table. The 'WHERE clause lets us l'ocus onjtlst the row we
w ant to remove. in this case one of FJmer’s customers who wants to he removed from the
mailing Iist.

The ... tual test witl.in a WHERE clause perlorms a comparison that is carried
out against every row in the table. In this examplc, the equ;il sign .=) lests
each value in the email coluinn lo see whieh rows are equal to "pr@honey-
doit.com". If the value in the email eoluntn of a row matehes. then that row
will be deleted.

Writo down why you think thi* intai 1 eoluntn is used in the WHERE
clause, as opposed to f irst_name or last_name:

148 Chapter 3

www.it-ebooks.info

create and populate a database

Miniwize the risk of accidental deletioHs


lt's importaitt to understand that although any coluinn name can IJC used in a
WHERE clause to mateh rows, there’s a very good reason why we chose the
A WHERE clause in a DELETE
emai \ coluran for Elmer’s DELETF. query. Consider that if more than one
row matehes a WHERE clause, all of the matehing rows will he deleted. So it's
statement lets you pinpoint the row
important for Elmer’s WHERE clause to pinpoint exactly the row you want to
delete. jou want to remove.
What we’re really talking abon t is uniqueness. It's fairly sale to assume that
email acidresses are uniqtic within the ema i 1 _ 1 ist table, whereas lirst
names and last naines are not. You don’t want to create a WHERE clause
matehing the first_name eoluntn lo "Pat" jusi to delete a single customer
you’ll end up deleting every customer named Pat! Fhat's why Elmer's WHERE
clause is carefully crafted to look for a spe. ific mateh with the email column.

DELETE FROM email_list


- V”.- ' UI
WHERE email = 'pr0honey-doit.com' tbe database .ni
m
ysql> DELETE FROM email_list WHERE email = 'pr0 honey-doit.com'; 1 row to be seen ajain

MsinJ tbe email tolumn m tbe


WHERE tlause belys to ertabl.sk ioe_m@storburxcoffee.cony
uni^weness and redute tbe risk of u...<.o@rhocohol.c-ioc.com
ate.dentally deletmj a row
hnrt.eh@obiecWille.net
Q,eqeck@breokneckpirro.com^
wilmowu@slorburzcoffee.com_
.nminffe@storfauzzcoffee.com
If we vred f.rrt_ndme m the ls@obiectville.net
WHERE £ la use instead of bshokes@mightygumboll nel_
ema.|, th.s user would
rhnan.@tikibeonlounge.cony
aeeidentally jet deleted
r|rnmmetp@simuduck.com_
deleted (0 . 0 0 5 sec)
you are here ► 149

www.it-ebooks.info

test-drive the DELETE eommand

DRII /0 --------------------------------------
Try out the DELETE eommand on Elmer’s database.
Fire up a MVSQL tool and try a few DELETE commands to delete individual n.ws
uf data from the email Iist lable bascd on customers' email addresses. Just make
sure to include a VIHERE clause on each DELETE statement so that you don't
accidentally wipe out the wliole table)
That’s right. Deleting users by hand with individual queries is no way to manage a mailing Iist.
Since Elmer will inevilably lace users who want to be removed from his mailing Iist in the liiture. it makes a lot of sense (o
rlevelop a welj-based user interface for removing customers. An HTML web form and l’HI’ seript shotild do the trick, not to

mention a DELETE FROM query with a little help from a WHERE i lause...

150 Chapter 3

S-ar putea să vă placă și