It returns an error message if it is unable to connect to the database.. “\n”; } // end if return $dbConn; } // end connectToSpy The connectToSpyfunction returns a connection to the data
Trang 1The mainButton()function uses the value of $mainProgramto build a link back to the primary screen in every other document produced by the system.
Connecting to the Database
The connectToSpy() function is fundamental to the spy system It uses system-level variables to generate a database connection It returns an error message if
it is unable to connect to the database The mysql_error()function prints an SQL error message if the data connection was unsuccessful This information may not
be helpful to the end user, but it might give you some insight as you are debug-ging the system.
function connectToSpy(){
//connects to the spy DB
global $serverName, $userName, $password;
$dbConn = mysql_connect($serverName, $userName, $password);
if (!$dbConn){
print “<h3>problem connecting to database </h3>\n”;
} // end if
$select = mysql_select_db(“chapter12”);
if (!$select){
print mysql_error() “<br>\n”;
} // end if
return $dbConn;
} // end connectToSpy
The connectToSpy()function returns a connection to the database that is subse-quently used in the many queries passed to the database throughout the system’s life span.
Creating a Quick List from a Query
I created a few functions in the spyMasterlibrary that didn’t get used in the pro-ject’s final version The qToList()function is a good example This program takes any SQL query and returns a simply formatted HTML segment describing the data I find this format useful when debugging because no complex formatting gets in the way.
function qToList($query){
//given a query, makes a quick list of data
global $dbConn;
l u
Trang 2$output = “”;
$result = mysql_query($query, $dbConn);
//print “dbConn is $dbConn<br>”;
//print “result is $result<br>”;
while ($row = mysql_fetch_assoc($result)){
foreach ($row as $col=>$val){
$output = “$col: $val<br>\n”;
} // end foreach
$output = “<hr>\n” ;
} // end while
return $output;
} // end qToList
Building an HTML Table from a Query
The qToTable() function is a little more powerful than qToList() It can build
an HTML table from any valid SQL SELECT statement The code uses the
mysql_fetch_field()function to determine field names from the query result It
also steps through each row of the result, printing an HTML row corresponding
to the record.
function qToTable($query){
//given a query, automatically creates an HTML table output
global $dbConn;
$output = “”;
$result = mysql_query($query, $dbConn);
$output = “<table border = 1>\n”;
//get column headings
//get field names
$output = “<tr>\n”;
while ($field = mysql_fetch_field($result)){
$output = “ <th>$field->name</th>\n”;
} // end while
$output = “</tr>\n\n”;
//get row data as an associative array
while ($row = mysql_fetch_assoc($result)){
409
i l
i o
Trang 3$output = “<tr>\n”;
//look at each field foreach ($row as $col=>$val){
$output = “ <td>$val</td>\n”;
} // end foreach
$output = “</tr>\n\n”;
}// end while
$output = “</table>\n”;
return $output;
} // end qToTable
The viewQuery.php program calls the qToTable() function, but it could be used anytime you want an SQL query formatted as an HTML table (which turns out to
be quite often).
Building an HTML Table for Editing an SQL Table
If the user has appropriate access, she should be allowed to add, edit, or delete records in any table of the database While qToTable()is suitable for viewing the results of any SQL query, it does not provide these features The tToEdit()function
is based on qToTable()with a few differences:
• tToEdit()does not accept a query, but the name of a table You cannot edit joined queries directly, only tables, so this limitation is sensible. tToEdit()creates a query that returns all records in the specified table
• In addition to printing the table data, tToEdit()adds two forms to each record
• One form contains all the data needed by the editRecord.phpprogram
to begin the record-editing process
• The other form added to each record sends all data necessary for deleting a record and calls the deleteRecord.phpprogram
One more form at the bottom of the HTML table allows the user to add a record
to this table This form contains information that the addRecord.php program needs.
function tToEdit($tableName){
//given a table name, generates HTML table including
//add, delete and edit buttons
global $dbConn;
l u
Trang 4$output = “”;
$query = “SELECT * FROM $tableName”;
$result = mysql_query($query, $dbConn);
$output = “<table border = 1>\n”;
//get column headings
//get field names
$output = “<tr>\n”;
while ($field = mysql_fetch_field($result)){
$output = “ <th>$field->name</th>\n”;
} // end while
//get name of index field (presuming it’s first field)
$keyField = mysql_fetch_field($result, 0);
$keyName = $keyField->name;
//add empty columns for add, edit, and delete
$output = “<th></th><th></th>\n”;
$output = “</tr>\n\n”;
//get row data as an associative array
while ($row = mysql_fetch_assoc($result)){
$output = “<tr>\n”;
//look at each field
foreach ($row as $col=>$val){
$output = “ <td>$val</td>\n”;
} // end foreach
//build little forms for add, delete and edit
//delete = DELETE FROM <table> WHERE <key> = <keyval>
$keyVal = $row[“$keyName”];
$output = <<< HERE
<td>
<form action = “deleteRecord.php”>
<input type = “hidden”
name = “tableName”
value = “$tableName”>
411
i l
i o
Trang 5<input type= “hidden”
name = “keyName”
value = “$keyName”>
<input type = “hidden”
name = “keyVal”
value = “$keyVal”>
<input type = “submit”
value = “delete”></form>
</td>
HERE;
//update: won’t update yet, but set up edit form
$output = <<< HERE
<td>
<form action = “editRecord.php”
method = “post”>
<input type = “hidden”
name = “tableName”
value = “$tableName”>
<input type= “hidden”
name = “keyName”
value = “$keyName”>
<input type = “hidden”
name = “keyVal”
value = “$keyVal”>
<input type = “submit”
value = “edit”></form>
</td>
HERE;
$output = “</tr>\n\n”;
}// end while
//add = INSERT INTO <table> {values}
//set up insert form send table name
$keyVal = $row[“$keyName”];
$output = <<< HERE
l u