MySQL
  Home arrow MySQL arrow Page 4 - Online Photo Album Development using 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 
Actuate Whitepapers 
VeriSign Whitepapers 
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: 3 stars3 stars3 stars3 stars3 stars / 31
    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:
      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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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 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...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...





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