Home arrow MySQL arrow 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.

TABLE OF CONTENTS:
  1. Online Photo Album Development using PHP and GD: Part 2
  2. Code: Explained
  3. Configurating
  4. Connecting to the Database
  5. 1,2,3, Testing
By: Frank Manno
Rating: starstarstarstarstar / 34
April 22, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MYSQL ARTICLES

- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: