<html>
<head>
<title>Update Record</title>
</head>
<body>
<h2>Update Record</h2>
<?
include “spyLib.php”;
$dbConn = connectToSpy();
$fieldNames = “”;
$fieldValues = “”;
foreach ($_REQUEST as $fieldName => $value){
if ($fieldName == “tableName”){
$theTable = $value;
} else {
$fields[] = $fieldName;
$values[] = $value;
} // end if
} // end foreach
print updateRec($theTable, $fields, $values);
print mainButton();
?>
</body>
</html>
It is more convenient for the updateRec() function if the field names and values
are sent as arrays. Therefore, the PHP code in
updateRecord.php converts the
$_REQUEST array to an array of fields and another array of values. These two arrays
are passed to the
updateRec() function, which processes them.
403
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
Viewing the deleteRecord.php Program
The deleteRecord.php program acts in a now-familiar manner. It mainly serves as
a wrapper for a function in the
spyLib library. In this particular case, the pro-
gram simply sends the name of the current table, the name of the key field, and
the value of the current record’s key to the
delRec() function. That function
deletes the record and returns a message regarding the success or failure of the
operation.
<html>
<head>
<title>Delete Record</title>
</head>
<body>
<h2>Delete Record</h2>
<?
include “spyLib.php”;
$dbConn = connectToSpy();
print delRec($tableName, $keyName, $keyVal);
print mainButton();
?>
</body>
</html>
Viewing the addRecord.php Program
Adding a record, which requires two distinctive steps, is actually much like edit-
ing a record. The
addRecord.php program calls the tToAdd() function, which
builds a form allowing the user to add data to whichever table is currently
selected. It isn’t necessary to send any information except the name of the table
to this function, because
tToAdd() automatically generates the key value.
<html>
<head>
<title>Add a Record</title>
</head>
<body>
<h2>Add Record</h2>
<?
404
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
include “spyLib.php”;
$dbConn = connectToSpy();
print tToAdd($tableName);
print mainButton();
?>
</body>
</html>
Viewing the processAdd.php Program
The tToAdd() function called by the addRecord.php program doesn’t actually add a
record. Instead, it places an HTML form on the screen that allows the user to enter
the data for a new record. When the user submits this form, he is passed to the
processAdd.php program, which calls procAdd() in the library code. The procAdd()
function generates the appropriate SQL code to add the new record to the table.
In order to do this,
procAdd() needs to know the field names and values. The names
and values are passed to the function in arrays just like in
updateRecord.php.
<html>
<head>
<title>Process Add</title>
</head>
<body>
<h2>Process Add</h2>
<?
include “spyLib.php”;
$dbConn = connectToSpy();
$fieldNames = “”;
$fieldValues = “”;
foreach ($_REQUEST as $fieldName => $value){
if ($fieldName == “tableName”){
$theTable = $value;
} else {
$fields[] = $fieldName;
405
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
406
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
$values[] = $value;
} // end if
} // end foreach
print procAdd($theTable, $fields, $values);
print mainButton();
?>
</body>
</html>
Creating the spyLib Library Module
Although I have described several PHP programs in this chapter, most of them
are simple. The
spyLib library code does most of the heavy lifting. Having a
library like
spyLib makes data programming pretty easy, because you don’t have
to know all the
spyLib details to make it work. All you need is a basic under-
standing of the functions in the library, what each function expects as input, and
what it will produce as output.
Although this library has a good amount of code (over 500 lines, in fact), there are
no new concepts in the library code. It’s worth looking carefully at this code
because it can give you a good idea of how to create your own libraries. You also find
there’s no better way to understand the library than to dig around under the hood.
Setting a CSS Style
Some of the simplest elements can have profound effects. One example of this
maxim is the storage of a CSS style in the library code. Each program in the system
operates using the style specified in the library. This means you can easily change
the look and feel of the entire system by manipulating one
<style></style>
block.
<style type = “text/css”>
body{
background-color: black;
color: white;
text-align:center
}
</style>
When you include a file, it is interpreted as HTML, not PHP. This means you can
place any HTML code in an include file and it is automatically inserted in your
output wherever the include function occurred. I took advantage of this fact to
include a CSS block in the library. If you want PHP code in your library file,
surround your code with PHP tags (<? ?>) in the library file.
Setting Systemwide Variables
Another huge advantage of a library file is the ability to set and use variables that
have meaning throughout the entire system. Since each PHP program in the sys-
tem includes the library, all have access to any variables declared in the library
file’s main section. Of course, you need to use the
global keyword to access a
global variable from within a function.
<?
//spyLib.php
//holds utilities for spy database
//variables
$userName = “”;
$password = “”;
$serverName = “localhost”;
$dbName = “chapter12”;
$dbConn = “”;
$adminPassword = “absolute”;
$mainProgram = “spyMaster.php”;
I stored a few key data points in the systemwide variables. The $userName, $password,
and
$serverName variables set up the data connection. I did this because I expect
people to reuse my library for their own databases. They definitely need to
change this information to connect to their own copy of MySQL. It’s much safer
for them to change this data in variables than in actual program code. If you’re
writing code for reuse, consider moving anything the code adopter might change
into variables.
The
$adminPassword variable holds the password used to edit data in the system.
Again, I want anybody reusing this library (including me) to change this value
without having to dig through the code.
The
$mainProgram variable holds the URL of the “control pad” program of the sys-
tem. In the
spy system, I want to provide access to spyMaster.php in every screen.
HINT
407
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