T
his book begins by looking at HTML pages, which are essentially static
documents. It then reveals how to generate dynamic pages with the
powerful PHP language. The last few chapters show how to use a database
management system such as MySQL to build powerful data structures. This chapter
ties together the PHP programming and data programming aspects to build a full-blown
data-management system for the
spy database. The system you learn can easily be
expanded to any kind of data project you can think of, including e-commerce
applications. Specifically, you learn how to:
• Design a moderate-to-large data application
• Build a library of reusable data functions
• Optimize functions for use across data sets
• Include library files in your programs
There isn’t really much new PHP or MySQL code to learn in this chapter. The focus is
on building a larger project with minimum effort.
B
u
i
l
d
i
n
g
a
T
h
r
e
e
-T
i
e
r
e
d
D
a
t
a
A
p
p
l
i
c
a
t
i
o
n
12
CHAPTER
384
P
H
P
5
/M
y
S
Q
L
P
r
o
g
r
a
m
m
i
n
g
f
o
r
t
h
e
A
b
s
o
l
u
t
e
B
e
g
i
n
n
e
r
Introducing the SpyMaster Program
The SpyMaster program is a suite of PHP programs that allows access to the spy
database created in chapter 11, “Data Normalization.” While the database cre-
ated in that chapter is flexible and powerful, it is
not
easy to use unless you know
SQL. Even if your users do understand SQL, you don’t want them to have direct
control of a database, because too many things can go wrong.
You need to build some sort of front-end application to the database. In essence,
this system has three levels.
• The client computer handles communication with the user.
• The database server (MySQL) manages the data.
• The PHP program acts as interpreter between the client and database.
PHP provides the bridge between the client’s HTML language and the
database’s SQL language.
This kind of arrangement is frequently called a
three-tier architecture
. As you
examine the
SpyMaster program throughout this chapter, you learn some of the
advantages of this particular approach.
Viewing the Main Screen
Start by looking at the program from the user’s point of view as shown in Figure 12.1.
The main page has two sections. The first is a series of data requests. Each of
these requests maps to a query.
Viewing the Results of a Query
When the user selects a query and presses the Submit button, a screen like the
one in Figure 12.2 appears.
The queries are all prebuilt, which means the user cannot make a mistake by
typing in inappropriate SQL code. It also limits the usefulness of the database.
Fortunately, you can add new queries.
385
C
h
a
p
t
e
r 1
2B
u
i
l
d
i
n
g
a
T
h
r
e
e
-T
i
e
r
e
d
D
a
t
a
A
p
p
l
i
c
a
t
i
o
n
FIGURE 12.1
The entry point to
the SpyMaster
database is clean
and simple.
FIGURE 12.2
The results of the
query are viewed in
an HTML table.
Viewing Table Data
The other part of the main screen (shown again in Figure 12.3) allows the user to
directly manipulate data in the tables. Since this is a more powerful (and thus dan-
gerous) enterprise, access to this part of the system is controlled by a password.
As an example, by selecting the
agent table I see a screen like Figure 12.4.
386
P
H
P
5
/M
y
S
Q
L
P
r
o
g
r
a
m
m
i
n
g
f
o
r
t
h
e
A
b
s
o
l
u
t
e
B
e
g
i
n
n
e
r
FIGURE 12.3
From the main
screen you can
also access the
table data with
a password.
FIGURE 12.4
The editTable
screen displays all
the information
in a table.
387
C
h
a
p
t
e
r 1
2B
u
i
l
d
i
n
g
a
T
h
r
e
e
-T
i
e
r
e
d
D
a
t
a
A
p
p
l
i
c
a
t
i
o
n
From this screen, the user can see all the data in the chosen table. The page also
gives the user links to add, edit, or delete records from the table.
Editing a Record
If the user chooses to edit a record, a screen similar to Figure 12.5 appears.
The Edit Record page has some important features. First, the user cannot directly
change the primary key. If she could do so, it would have profound destabilizing
consequences on the database. Also note the way the
operationID field is pre-
sented. The field itself is a primary key with an integer value, but it would be very
difficult for a user to directly manipulate the integer values. Instead, the pro-
gram provides a drop-down list of operations. When the user chooses from this
list, the appropriate numerical index is sent to the next page.
Confirming the Record Update
When the user clicks the button, a new screen appears and announces the suc-
cessful update as in Figure 12.6.
Deleting a Record
The user can also choose to delete a record from the Edit Table page. This action
results in the basic screen shown in Figure 12.7.
FIGURE 12.5
The user is editing
a record in the
agent table.