// restrictions on table design //foreign keys MUST

October 29, 2006 on 12:06 pm | In php | No Comments

HERE; } else if (preg_match( /(.*)ID$/ ?, $col, $match)) { //it s a foreign key reference// get table name (match[1]) //create a listbox based on table name and its name field$valList = fieldToList($match[1],$col, $fieldNum, name ?); $output .= << $col $valList

HERE; } else { $output .= << $col

HERE; } // end if$fieldNum++; } // end foreach$output .= <<< HERE

Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services

// restrictions on table design //foreign keys MUST

October 29, 2006 on 12:06 pm | In php | No Comments

// restrictions on table design //foreign keys MUST be named tableID where table is table name// (because mySQL doesn t recognize foreign key indicators) // I also expect a name field in any table used as a foreign key// (for same reason) global $dbConn; $output = ?; $result = mysql_query($query, $dbConn); $row = mysql_fetch_assoc($result); //get table name from field object$fieldObj = mysql_fetch_field($result, 0); $tableName = $fieldObj->table; $output .= <<< HERE


HERE; $fieldNum = 0; foreach ($row as $col=>$val){ if ($fieldNum == 0){ //it s primary key. don t make textbox, //but store value in hidden field instead//user shouldn t be able to edit primary keys$output .= <<

416PHP5/MySQLProgrammingfortheAbsoluteBeginner
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services

function rToEdit ($query){ //given a one-record query, creates

October 29, 2006 on 2:17 am | In php | No Comments

HERE; foreach ($row as $col=>$val){ $output .= <<

HERE; } // end foreach$output .= <<< HERE

$col $val
$col

HERE; return $output; } // end rToEditBuilding a Smarter Edit FormThe smartRToEdit()function builds on the basic design of rToEdit()but com- pensates for a couple of major flaws in the rToEdit()design. Take a look at thesmarter code: function smartRToEdit ($query){ //given a one-record query, creates a form to edit that record//Doesn t let user edit first (primary key) field//generates dropdown list for foreign keys//MUCH safer than ordinary rToEdit function415Chapter

Hint: This post is supported by Gama web hosting hrvatska services

function rToEdit ($query){ //given a one-record query, creates

October 29, 2006 on 2:17 am | In php | No Comments

function rToEdit ($query){ //given a one-record query, creates a form to edit that record//works on any table, but allows direct editing of keys//use smartRToEdit instead if you canglobal $dbConn; $output = ?; $result = mysql_query($query, $dbConn); $row = mysql_fetch_assoc($result); //get table name from field object$fieldObj = mysql_fetch_field($result, 0); $tableName = $fieldObj->table; $output .= <<< HERE

414PHP5/MySQLProgrammingfortheAbsoluteBeginnerFIGURE 12.12TherToEditfunction is simplebut producesdangerous output.

Hint: This post is supported by Gama web hosting hrvatska services

HERE; //update: won t update yet, but set up

October 28, 2006 on 2:13 pm | In php | No Comments

HERE; $output .=


\n ?; return $output; } // end tToEditLook carefully at the forms for editing and deleting records. These forms containhidden fields with the table name, key field name, and record number. Thisinformation will be used by subsequent functions to build a query specific to therecord associated with that particular table row. Creating a Generic Form to Edit a RecordThe table created in tToEdit()calls a program called editRecord.php. This pro- gram accepts a one-record query. It prints out an HTML table based on the resultsof that query. The output of rToEdit()is shown in Figure 12.12. The rToEditfunction produces a very simple HTML table. Every field has a corre- sponding textbox. The advantage of this approach is that it works with any table. However, the use of this form is quite risky. The user should not be allowed to change the primary key, because thatwould edit some other record, which could have disastrous results. The operationIDfield is a foreign key reference. The only valid entries tothis field are integers corresponding to records in the operationtable. There s no way for the user to know what operation a particular integer isrelated to. Worse, she could enter any number (or any text) into the field. The results would be unpredictable, but almost certainly bad. I fix these defects in the smartRToEdit()function coming up next, but begin bystudying this simpler function, because smartRToEdit()is built on rToEdit().
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services

HERE; //update: won t update yet, but set up

October 28, 2006 on 2:13 pm | In php | No Comments

HERE; //update: won t update yet, but set up edit form$output .= <<< HERE

HERE; $output .=

\n\n ?; }// end while//add = INSERT INTO

{values} //set up insert form send table name$keyVal = $row[ $keyName ?]; $output .= <<< HERE412PHP5/MySQLProgrammingfortheAbsoluteBeginner
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services

$output .= \n ?; //look at each fieldforeach ($row

October 28, 2006 on 4:30 am | In php | No Comments

$output = ?; $query = SELECT * FROM $tableName ?; $result = mysql_query($query, $dbConn); $output .=

\n ?; //get column headings//get field names$output .=
\n ?; while ($field = mysql_fetch_field($result)){ $output .=

\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 .=

\n ?; $output .=

\n\n ?; //get row data as an associative arraywhile ($row = mysql_fetch_assoc($result)){ $output .=

\n ?; //look at each fieldforeach ($row as $col=>$val){ $output .=

\n ?; } // end foreach//build little forms for add, delete and edit//delete = DELETE FROM

$field->name
$val
WHERE = $keyVal = $row[ $keyName ?]; $output .= <<< HERE \n ?; //look at each fieldforeach ($row as $col=>$val){ $output .=

\n ?; } // end foreach$output .=

\n\n ?; }// end while$output .=

Hint: This post is supported by Gama hrvatski web hosting services

$output .= \n ?; //look at each fieldforeach ($row

October 28, 2006 on 4:30 am | In php | No Comments

$output .=

$val

\n ?; return $output; } // end qToTableThe viewQuery.phpprogram calls the qToTable()function, but it could be usedanytime you want an SQL query formatted as an HTML table (which turns out tobe quite often). Building an HTML Table for Editing an SQL TableIf the user has appropriate access, she should be allowed to add, edit, or deleterecords in any table of the database. While qToTable()is suitable for viewing theresults of any SQL query, it does not provide these features. The tToEdit()functionis based on qToTable()with a few differences: tToEdit()does not accept a query, but the name of a table. You cannotedit 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 eachrecord. One form contains all the data needed by the editRecord.phpprogramto 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 recordto this table. This form contains information that the addRecord.phpprogramneeds. function tToEdit($tableName){ //given a table name, generates HTML table including//add, delete and edit buttonsglobal $dbConn; 410PHP5/MySQLProgrammingfortheAbsoluteBeginner

Hint: This post is supported by Gama hrvatski web hosting services

The mainButton()function uses the value of $mainProgramto build

October 27, 2006 on 6:14 pm | In php | No Comments

$output = ?; $result = mysql_query($query, $dbConn); //print dbConn is $dbConn
?; //print result is $result
?; while ($row = mysql_fetch_assoc($result)){ foreach ($row as $col=>$val){ $output .= $col: $val
\n ?; } // end foreach$output .=


\n ? ; } // end whilereturn $output; } // end qToListBuilding an HTML Table from a QueryThe qToTable()function is a little more powerful than qToList(). It can build an HTML table from any valid SQL SELECTstatement. The code uses themysql_fetch_field()function to determine field names from the query result. Italso steps through each row of the result, printing an HTML row correspondingto the record. function qToTable($query){ //given a query, automatically creates an HTML table outputglobal $dbConn; $output = ?; $result = mysql_query($query, $dbConn); $output .=
\n ?; //get column headings//get field names$output .=
\n ?; while ($field = mysql_fetch_field($result)){ $output .=

\n ?; } // end while$output .=

\n\n ?; //get row data as an associative arraywhile ($row = mysql_fetch_assoc($result)){

Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services

The mainButton()function uses the value of $mainProgramto build

October 27, 2006 on 6:14 pm | In php | No Comments

The mainButton()function uses the value of $mainProgramto build a link back tothe primary screen in every other document produced by the system. Connecting to the DatabaseThe connectToSpy()function is fundamental to the spysystem. It uses system- level variables to generate a database connection. It returns an error message ifit is unable to connect to the database. The mysql_error()function prints an SQLerror message if the data connection was unsuccessful. This information may notbe 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 DBglobal $serverName, $userName, $password; $dbConn = mysql_connect($serverName, $userName, $password); if (!$dbConn){ print

problem connecting to database…

\n ?; } // end if$select = mysql_select_db( chapter12 ?); if (!$select){ print mysql_error() .
\n ?; } // end ifreturn $dbConn; } // end connectToSpyThe connectToSpy()function returns a connection to the database that is subse- quently used in the many queries passed to the database throughout the system slife span. Creating a Quick List from a QueryI 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 takesany SQL query and returns a simply formatted HTML segment describing thedata. I find this format useful when debugging because no complex formattinggets in the way. function qToList($query){ //given a query, makes a quick list of dataglobal $dbConn; 408PHP5/MySQLProgrammingfortheAbsoluteBeginner

Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services

« Previous PageNext Page »

Powered by cheap hosting

$field->name