PHP
  Home arrow PHP arrow Databases: Finishing a Listing Service
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Databases: Finishing a Listing Service
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-07-05

    Table of Contents:
  • Databases: Finishing a Listing Service
  • Adding a Business
  • Displaying the Database
  • PHP Data Objects
  • PDO and prepared statements

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Databases: Finishing a Listing Service


    (Page 1 of 5 )

    Concluding our discussion of databases and PHP, we'll finish building the example that we started last week. This article is excerpted from chapter eight of the book Programming PHP, Second Edition, written by Kevin Tatroe, Rasmus Lerdorf, and Peter MacIntyre (O'Reilly, 2006; ISBN: 0596006810). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Administrator’s Page 

    Example 8-4 shows the backend page that allows administrators to add categories to the listing service. The input fields for adding a new record appear after a dump of the current data. The administrator fills in the form and presses the Add Category button, and the page redisplays with the new record. If any of the three fields are not filled in, the page displays an error message.

    Example 8-4.  Backend administration page

    <html>
    <head>
    <?php
     
    require_once('db_login.php');
    ?>

    <title>
    <?php
     // print the window title and the topmost body heading
     $doc_title = 'Category Administration';
     echo "$doc_title\n";
    ?>
    </title>
    </head>
    <body>
    <h1>
    <?php
     
    echo "$doc_title\n";
    ?>
    </h1>

    <?php
     // add category record input section

     // extract values from $_REQUEST
     $Cat_ID = $_REQUEST['Cat_ID'];
     $Cat_Title = $_REQUEST['Cat_Title'];
     $Cat_Desc = $_REQUEST['Cat_Desc'];
     $add_record = $_REQUEST['add_record'];

     // determine the length of each input field
     $len_cat_id = strlen($_REQUEST['Cat_ID']);
     $len_cat_tl = strlen($_REQUEST['Cat_Title']);
     $len_cat_de = strlen($_REQUEST['Cat_Desc']);

     // validate and insert if the form script has been
     // called by the Add Category button
     if ($add_record == 1) {
        
    if (($len_cat_id > 0) and ($len_cat_tl > 0) and ($len_cat_de > 0)){
             $sql = "insert into categories (category_id, title, description)";
             $sql .= " values ('$Cat_ID', '$Cat_Title', '$Cat_Desc')";
             $result = $db->query($sql);
             $db->commit();
         } else {
         echo "<p>Please make sure all fields are filled in ";
         echo "and try again.</p>\n";
         } 
     
    }

     // list categories reporting section

     // query all records in the table after any
     // insertion that may have occurred above
     $sql = "select * from categories";
     $result = $db->query($sql);
    ?>

    <form method="post" action="<?= $PHP_SELF
    ?>">

    <table>
    <tr><th bgcolor="#eeeeee">Cat ID</th>
        <th bgcolor="#eeeeee">Title</th>
        <th bgcolor="#eeeeee">Description</th>
    </tr>

    <?php
     // display any records fetched from the database
     // plus an input line for a new category
     while ($row = $result->fetchRow()){
        
    echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr>\n";
     }
    ?>

    <tr><td><input type="text" name="Cat_ID" size="15" maxlength="10" /></td>
        <td><input type="text" name="Cat_Title" size="40" maxlength="128" /></td>
        <td><input type="text" name="Cat_Desc" size="45" maxlength="255" /></td>
    </tr>
    </table>
    <input type="hidden" name="add_record" value="1" />
    <input type="submit" name="submit" value="Add Category" />
    </body>
    </html>

    When the administrator submits a new category, we construct a query to add the category to the database. Another query displays the table of all current categories. Figure 8-4 shows the page with five records loaded.

    More PHP Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming PHP, Second Edition,"...
     

    Buy this book now. This article is excerpted from chapter eight of the book Programming PHP, Second Edition, written by Kevin Tatroe, Rasmus Lerdorf, and Peter MacIntyre (O'Reilly, 2006; ISBN: 0596006810). Check it out today at your favorite bookstore. Buy this book now.

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT