MySQL
  Home arrow MySQL arrow Page 4 - Online Photo Album Development using PHP and GD: Part 2
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
MYSQL

Online Photo Album Development using PHP and GD: Part 2
By: Frank Manno
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 34
    2004-04-22


    Table of Contents:
  • Online Photo Album Development using PHP and GD: Part 2
  • Code: Explained
  • Configurating
  • Connecting to the Database
  • 1,2,3, Testing

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Online Photo Album Development using PHP and GD: Part 2 - Connecting to the Database
    ( Page 4 of 5 )

    The first thing we do is check to see if the album name and description is incomplete (empty). If they are empty, we call the displayPage() function and pass to it our error message. The displayPage() function simply accepts a string argument, and displays the message to the user. Once the displayPage() function has been called, we call the die() function, which simply terminates the script's execution.

    If the album name and description has been completed, we connect to our database by calling the db_connect() function, located in our config.php script.

    // Connect to database

     
    <br />db_connect(); 
    <
    br />$sql "INSERT INTO albums VALUES(0, '" addslashes($_POST['album_name']) . 
    "', '" addslashes($_POST['album_desc']) . "', '')"
    <
    br />$result = @mysql_query($sql) or die("Error inserting record: " mysql_error());

    Once connected, we create and execute our SQL statement, which simply inserts the values passed from the form into our album table. We deliberately leave the value of the thumbnail location empty; this value will be populated when uploading our album images.

     
    <p>if ($result){ 
    <
    br />// Notify use that album was successfully created. 
    <br />$msg .= "Album <strong>" $_POST['album_name'] . "</strong> successfully created!"
    <
    br />$msg .= 
    <br /><a href="
    /administrator/'edit_album.php?album_id=">Click here</a> to administrate the " . $_POST['album_name'] . " album"; 
    <br />$msg .= " </p>
    <p><a href="/administrator/'
    index.php'">Click here</a> to return to the administrative area</p>"; 
    <br />displayPage($msg); 
    <p> </p>"; displayPage($msg);";displayPage($msg); 
    <p>If our insert query is successfully executed, we call the displayPage() function, 
    and pass it our string argument; in this case, a notice to the user that the album 
    has been added. We also indicate options for administrating the album (ie: adding 
    pictures, etc.).</p>
    <p>Now that our add_album.php script is complete, we can add functionality to edit 
    our existing albums. We'
    ll create a script called edit_albums.php:</p>
    <
    p><!--p<-->include_once("../include/config.php"); 
    <
    br />// Has album been updated? 
    <br />if ( $_POST['edit'] ){ 
    <
    br />if ( empty($_POST['album_name']) || empty($_POST['album_desc'])){ 
    <
    br />$msg "Please complete all required fields! 
    <br /><a href="
    /administrator/'new_album.php'">Go Back</a>"
    <
    br />displayPage$msg"Error Updating Album!"); 
    <
    br />die(); 
    <
    br />} 
    <
    br />db_connect(); 
    <
    br />// Insert updated record into DB 
    <br />$sql "UPDATE albums SET album_name = '" addslashes($_POST['album_name']) 
    "', album_desc = '" addslashes($_POST['album_desc']) . "' WHERE album_id = 
    addslashes($_POST['album_id']); 
    <
    br />$result = @mysql_query$sql ) or die("Error inserting record: " mysql_error()); 

    <
    br />if ($result){ 
    <
    br />$msg "Album updated successfully! 
    <br /><a href="
    /administrator/'index.php'">Return to Admin Menu</a>"
    <
    br />displayPage($msg"Album Updated Successfully!"); 
    <
    br />die(); 
    <
    br />} 
    <
    br />} else if ( !$_POST['edit'] && !empty($_GET['album_id'])){ 
    <
    br />db_connect(); 
    <
    br />// Retrieve album information 
    <br />$sql "SELECT album_id, album_name, album_desc FROM albums WHERE album_id = 
    addslahes($_GET['album_id']); 
    <
    br />$result = @mysql_query$sql ) or die("Error retrieving record: " mysql_error()); 

    <
    br />while($row mysql_fetch_array$result )){ 
    <
    br />// Display edit page 
    <br />$msg .= 
    <form action="
    edit_albums.php" method="post">"
    <
    br />$msg .= 
    <table cellspacing="
    0" cellpadding="5" width="60%" border="0">"
    <
    br />$msg .= 
    <tbody>
    <tr>
    <td>Album Name:</td>
    <td>
    <input id="
    album_name" type="text" size="40" value="" name="album_name" /></td></tr>"
    <
    br />$msg .= 
    <tr>
    <td>Album Description:</td>
    <td><textarea id="
    album_desc" name="album_desc" rows="4" cols="30">" $row['album_desc'] . "</textarea></td></tr>"
    <
    br />$msg .= 
    <tr>
    <td>
    <input type="
    hidden" value="1" name="edit" /> 
    <input type="
    hidden" value="" name="album_id" /></td>"
    <
    br />$msg .= 
    <td>
    <input id="
    submit" type="submit" value="Continue" name="submit" />"
    <
    br />$msg .= "<a href="/administrator/del_albums.php?album_id=">Delete</a>"
    <
    br />$msg .= "</td></tr></tbody></table></form>"
    <
    br />$album_name $row['album_name']; 
    <
    br />} 
    <
    br />displayPage($msg"Editing Album " $album_name ":"); 
    <
    br />// Display album summaries 
    <br />} elseif ( !$_GET['album_id'] ){ 
    <
    br />db_connect(); 
    <
    br />// Retrieve all album information 
    <br />$sql "SELECT album_id, album_name FROM albums"
    <
    br />$result = @mysql_query$sql ) or die( "Error retrieving records: " mysql_error() 
    ); 
    <
    br />$i 0
    <
    br />while($row mysql_fetch_array($result)){ 
    <
    br />if (( $i ) == && ( $i != )){ 
    <
    br />$msg .= (
    </tr />
    <tr />"
    ); 
    <
    br />} 
    <
    br />$msg .= (
    <td />" 
    . ($i 1) . ". <a href="/administrator/'edit_albums.php?album_id=">" . $row['album_name'] . " 
    </td />"); 
    <br />$i++; 
    <br />} 
    <br />displayPage( $msg, "Edit Albums", false ); 
    <br />} 
    <br />

     
    <p>Here'
    s a breakdown of the code: </p>
    <
    p>include_once("../include/config.php"); 
    <
    br />// Has album been updated? 
    <br />if ( $_POST['edit'] ){ 
    <
    br />if ( empty($_POST['album_name']) || empty($_POST['album_desc'])){ 
    <
    br />$msg "Please complete all required fields! 
    <br /><a href="
    /administrator/'new_album.php'">Go Back</a>"
    <
    br />displayPage$msg"Error Updating Album!"); 
    <
    br />die(); 
    <
    br />} 
    <
    br />db_connect(); 
    <
    br />// Insert updated record into DB 
    <br />$sql "UPDATE albums SET album_name = '" addslashes($_POST['album_name']) 
    "', album_desc = '" addslashes($_POST['album_desc']) . "' WHERE album_id = 
    addslashes($_POST['album_id']); 
    <
    br />$result = @mysql_query$sql ) or die("Error inserting record: " mysql_error()); 

    <
    br />if ($result){ 
    <
    br />$msg "Album updated successfully! 
    <br /><a href="
    /administrator/'index.php'">Return to Admin Menu</a>"
    <
    br />displayPage($msg"Album Updated Successfully!"); 
    <
    br />die(); 
    <
    br />}</p></a></p>
    </
    />"; displayPage( $msg, "Error Updating Album!"); die(); } db_connect(); // Insert 
    updated record into DB $sql = "
    UPDATE albums SET album_name '" . addslashes($_POST['album_name']) 
    . "'
    album_desc '" . addslashes($_POST['album_desc']) . "' WHERE album_id 
    " . addslashes($_POST['album_id']); $result = @mysql_query( $sql ) or die("Error 
    inserting record
    " . mysql_error()); if ($result){ $msg = "Album updated successfully
    "; displayPage($msg, "Album Updated Successfully!"); die(); } 
    </a />

    >

     
     
    >>> More MySQL Articles          >>> More By Frank Manno
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT