PHP
  Home arrow PHP arrow Page 6 - Building A Quick-And-Dirty PHP/MySQL P...
Dev Shed Forums 
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

Building A Quick-And-Dirty PHP/MySQL Publishing System
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 42
    2002-03-12

    Table of Contents:
  • Building A Quick-And-Dirty PHP/MySQL Publishing System
  • A Little Slug-gish
  • A Maniac Is Born
  • Bedtime Stories
  • Admin Ahoy!
  • Splitting Up
  • Erasing The Past
  • Changing Things Around
  • Game Over

  • 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


    Building A Quick-And-Dirty PHP/MySQL Publishing System - Splitting Up


    (Page 6 of 9 )

    Next, "add.php". If you think about it, you'll realize that this script actually has two components to it: a form, which displays fields for the administrator to enter information, and a form processor, which validates the input and inserts it into the database.

    Now, I could write this as two separate scripts; however, it's a habit of mine to put both components into the same script and wrap a conditional test around them to decide which one gets used when. So, the broad outline of my "add.php" script would look like this:

    <?
    // form not yet submitted
    // display initial form
    if (!$submit)
    {
    // code to display form goes here
    }
    // form submitted
    // process it
    else
    {
    // code to process form data goes here
    }
    ?>
    Based on the presence or absence of the $submit variable, the script can take a decision as to whether to display the initial form, or initiate the form processor.

    Now, when an administrator first accesses the page through a browser, the $submit variable will not exist, and so control will shift to the first section of the script, which displays an HTML form. Let's look at that first:

    <?
    // add.php - add a new press release
    ?>

    <!-- page header - snip -->

    <?
    // form not yet submitted
    // display initial form
    if (!$submit)
    {
    ?>
    <table cellspacing="5" cellpadding="5">
    <form action="<? echo $PHP_SELF; ?>" method="POST">
    <tr>
    <td valign="top"><b><font size="-1">Slug</font></b></td>
    <td><input size="50" maxlength="250" type="text" name="slug"></td>
    </tr>
    <tr>
    <td valign="top"><b><font size="-1">Content</font></b></td>
    <td><textarea name="content" cols="40" rows="10"></textarea></td>
    </tr>
    <tr>
    <td valign="top"><font size="-1">Contact person</font></td>
    <td><input size="50" maxlength="250" type="text" name="contact"></td>
    </tr>
    <tr>
    <td colspan=2><input type="Submit" name="submit" value="Add"></td>
    </tr>
    </form>
    </table>
    <?
    }
    else
    {
    // form processor code here
    }
    ?>

    <!-- page footer - snip -->
    Here's what it looks like:



    Now, once the administrator enters data into this form and submits it, the same script is called again to process the data (note the presence of the special $PHP_SELF variable in the form's ACTION attribute). Since the $submit variable will now exist, control will transfer to the latter half of the script, which looks like this:

    <?
    // add.php - add a new press release
    ?>

    <!-- page header - snip -->

    <?
    if (!$submit)
    {
    // form display code goes here
    }
    else
    {
    // includes
    include("../conf.php");
    include("../functions.php");

    // set up error list array
    $errorList = array();
    $count = 0;

    // validate text input fields
    if (!$slug) { $errorList[$count] = "Invalid entry: Slug"; $count++; }

    if (!$content) { $errorList[$count] = "Invalid entry: Content"; $count++; }

    // set default value for contact person
    if (!$contact) { $contact = $def_contact; }

    // check for errors
    // if none found...
    if (sizeof($errorList) == 0)
    {
    // open database connection
    $connection = mysql_connect($host, $user, $pass) or die ("Unable to
    connect!");

    // select database
    mysql_select_db($db) or die ("Unable to select database!");

    // generate and execute query
    $query = "INSERT INTO news(slug, content, contact, timestamp)
    VALUES('$slug', '$content', '$contact', NOW())";
    $result = mysql_query($query) or die ("Error in query: $query. " .
    mysql_error());

    // print result
    echo "<font size=-1>Update successful. <a href=list.php>Go back to the
    main menu</a>.</font>";

    // close database connection
    mysql_close($connection);
    }
    else
    {
    // errors found
    // print as list
    echo "<font size=-1>The following errors were encountered: <br>";
    echo "<ul>";
    for ($x=0; $x<sizeof($errorList); $x++)
    {
    echo "<li>$errorList[$x]";
    }
    echo "</ul></font>";
    }
    }
    ?>

    <!-- page footer - snip -->
    Fairly simple, this. The first thing to do is ensure that all required values are present, and to generate errors if not. These errors are stored in the array $errorList.

    <?
    // set up error list array
    $errorList = array();
    $count = 0;

    // validate text input fields
    if (!$slug) { $errorList[$count] = "Invalid entry: Slug"; $count++; }

    if (!$content) { $errorList[$count] = "Invalid entry: Content"; $count++; }
    ?>
    In the event that the contact person field is left empty, a default value is used; this value is pulled in from the configuration file "conf.php".

    <?
    // set default value for contact person
    if (!$contact) { $contact = $def_contact; }
    ?>
    Once all the data validation is complete, the $errorList array is checked for entries. If entries are present in this array, a message is displayed listing the errors; if not, an INSERT query is generated to add the data to the database, and a success message is printed to the browser.

    <?
    // check for errors
    // if none found...
    if (sizeof($errorList) == 0)
    {
    // snip

    // generate and execute query
    $query = "INSERT INTO news(slug, content, contact, timestamp)
    VALUES('$slug', '$content', '$contact', NOW())";
    $result = mysql_query($query) or die ("Error in query: $query. " .
    mysql_error());

    // print result
    echo "<font size=-1>Update successful. <a href=list.php>Go back to the
    main menu</a>.</font>";

    // snip
    }
    else
    {
    // errors found
    // print as list
    echo "<font size=-1>The following errors were encountered: <br>";
    echo "<ul>";
    for ($x=0; $x<sizeof($errorList); $x++)
    {
    echo "<li>$errorList[$x]";
    }
    echo "</ul></font>";
    }
    ?>

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway