Because web database applications are such a mainstay of web development, we’ve decided to show you a complete sample application in this chapter. This section develops a self-maintaining business listing service. Companies add their own records to the database and pick the category or categories by which they want to be indexed. Two HTML forms are needed to populate the database tables. One form provides the site administrator with the means to add category IDs, titles, and descriptions. The second form, used by the self-registering businesses, collects the business contact information and permits the registrant to associate the listing with one or more categories. A separate page displays the listings by category on the web page. Database Tables There are three tables: businesses to collect the address data for each business, categories to name and describe each category, and an associative table called biz_categories to relate entries in the other two tables to each other. These tables and their relationships are shown in Figure 8-3.
Example 8-2 contains a dump of the table schema in MySQL format. Depending on your database’s features, the schema may have to be altered slightly. Example 8-2. Database schema # ------------------------------------------ CREATE TABLE biz_categories ( # ------------------------------------------# CREATE TABLE businesses ( # ------------------------------------------# CREATE TABLE categories ( Database Connection We’ve designed these pages to work with a MySQL, PostgreSQL, or Oracle 8i backend. The only visible sign of this in the PHP code is that we use commit() after every update. We’ve abstracted the database-specific stuff to a db_login.php library, shown in Example 8-3, which selects an appropriate DSN for MySQL, PostgreSQL, or Oracle. Example 8-3. Database connection abstraction script (db_login.php) <?php // database connection setup section $username = 'user'; // select one of these three values for $phptype // $phptype = 'pgsql'; // check for Oracle 8 - data source name syntax is different if ($phptype != 'oci8'){ // establish the connection $db = DB::connect($dsn); Please check back next week for the conclusion to this article.
blog comments powered by Disqus |
|
|
|
|
|
|
|