HomeMySQL Page 3 - Online Photo Album Development using PHP and GD: Part 2
Configurating - 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.
Our config file initializes our connection variables: $db_server, $db_user, $db_pass, and $db_name. These variables are used in the db_connect() function which simply connects to our database system (in this case MySQL) and selects the album database we'll be using in our application.
Our create_album.php file is simply a form that accepts user-input, and submits the data to our processing script. Once the data is entered by the user, our processor script checks to see if all the fields have been completed; if not, an error message is displayed. If all fields are complete, the album data is added to the database. We'll call our processor script add_album.php:
<p><!--p<-->include_once("../include/config.php"); <br />// Verify that all form elements are completed <br />if (empty($_POST['album_name']) || empty($_POST['album_desc'])){ <br />displayPage("Please complete all required fields! <br /><a href="/administrator/'new_album.php'">Go Back</a>", "Error Adding Album!"); <br />die(); <br />} <br />// Connect to database <br />db_connect(); <br />$sql = "INSERT INTO albums VALUES(0, '" . addslashes($_POST['album_name']) . "', '" . addslashes($_POST['album_desc']) . "', 0, '', '')"; <br />$result = @mysql_query($sql) or die("Error inserting record: " . mysql_error());
<br />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, "Album " . $_POST['album_name'] . "Added!"); <br />} <br /> <p> </p>"; displayPage($msg, "Album " . $_POST['album_name'] . "Added!"); } ";displayPage($msg, "Album " . $_POST['album_name'] . "Added!");} <p>Here's a breakdown of the code:</p> <p>include_once("../include/config.php"); <br />// Verify that all form elements are completed <br />if (empty($_POST['album_name']) || empty($_POST['album_desc'])){ <br />displayPage("Please complete all required fields! <br /><a href="/administrator/'new_album.php'">Go Back</a>", "Error Adding Album!"); <br />die(); <br />}</p>
The include line, again, is to make use of the global variables and function that allow us to connect to the database.