HomeMySQL Page 4 - Online Photo Album Development using PHP and GD: Part 2
Connecting to the Database - MySQL
In this part Frank will explain how to code the photo album using PHP and MySQL. This is the second part of his series and focuses on building the user interface.
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.
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());