Here is the page where you can modify an album's name, description and image icon

We
display the album thumbnail here just to make sure we're updating the
right album and because i think the form is way too dull without any
images.
After you hit the "Modify" button we just need
to save the new name & description to the database. However, before
we changing the thumbnail we must first check if a new thumbnail image
is provided or not.
Here is the code snippet :
<!-- google_ad_section_start(weight=ignore) -->
// ... some code here
if(isset($_POST['txtName'])) {
$albumId = $_POST['hidAlbumId'];
$albumName = $_POST['txtName'];
$albumDesc = $_POST['mtxDesc'];
if (!get_magic_quotes_gpc()) {
$albumName = addslashes($albumName);
$albumDesc = addslashes($albumDesc);
}
if ($_FILES['fleImage']['tmp_name'] != '') {
$imgName = $_FILES['fleImage']['name'];
$tmpName = $_FILES['fleImage']['tmp_name'];
// just like when we add this album
// we will need to rename the image name to avoid
// duplicate file name problem
$newName = md5(rand() * time()) . strrchr($imgName, ".");
// resize the new album image
$result = createThumbnail($tmpName, ALBUM_IMG_DIR . $newName,
THUMBNAIL_WIDTH);
if (!$result) {
echo "Error uploading file";
exit;
}
// since a new image for this album is specified
// we'll need to delete the old one
$sql = "SELECT al_image
FROM tbl_album
WHERE al_id = $albumId";
$result = mysql_query($sql)
or die('Error, get album info failed. ' .
mysql_error());
$row = mysql_fetch_assoc($result);
unlink(ALBUM_IMG_DIR . $row['al_image']);
$newName = "'$newName'";
} else {
// don't change the image
$newName = "al_image";
}
$query = "UPDATE tbl_album
SET al_name = '$albumName',
al_description = '$albumDesc',
al_image = $newName
WHERE al_id = $albumId";
mysql_query($query)
or die('Error, modify album failed : ' .
mysql_error());
// after saving the modification go to the detail page
echo "<script>window.location.href='index.php?page=album-detail&alId=$albumId';</script>";
}
// ... more code down here
<!-- google_ad_section_end -->
As
you can see on the code above we update the album thumbnail only if a
new image is provided. The process is no different than when we add a
new album. After the new image is uploaded and the thumbnail is created
successfully we then delete the old one using unlink(). That's the php function you'll need when you want to delete a file.
Now. you need to take a look at the above code again but this time focus on this little piece
<!-- google_ad_section_start(weight=ignore) -->
// ... some code here
if ($_FILES['fleImage']['tmp_name'] != '') {
// ... upload the new image & delete the old one
$newName = "'$newName'";
} else {
$newName = "al_image";
}
$query = "UPDATE tbl_album
SET al_name = '$albumName',
al_description = '$albumDesc',
al_image = $newName
WHERE al_id = $albumId";
// ... and more down here
<!-- google_ad_section_end -->
If you're wondering why in the if block $newName is surrounded by single quotes but in the else block $newName is given the value "al_image" without the single quotes, here's the explanation. Note that in the sql query string $albumName and $albumDesc is surrounded by quotes but $newName isn't. We need to do this so that when the image is not changed we can set the query to leave the image alone.
Here's
an example so you can understand it better. For the first scenario
imagine that the admin update an album ( id = 123 ) and modify it's
name to "my new album", description is changed to "my new description"
and he also give a new image and it is saved under the new name
"3b6a267a967d7535ff3b1ebc3d9e3c1e.jpg". The script will go through the
if block because the value of $_FILES['fleImage']['tmp_name'] won't be empty and then the value of $query will be ...
UPDATE tbl_album
SET al_name = 'my new album',
al_description = 'my new description', al_image = '3b6a267a967d7535ff3b1ebc3d9e3c1e.jpg' WHERE al_id = 123
... which will update the image information including the image name.
For
the second scenario the admin only change the name and description but
leave the image alone.Now the script will go to the else block and the
value of $query will be ...
UPDATE tbl_album
SET al_name = 'my new album',
al_description = 'my new description', al_image = al_image WHERE al_id = 123
... which will update the album name and description but leave the old image name as it is.
This is certainly not
the only way to do it. You can easily add an if else statement and make
a new query for each scenario. I'm just showing an alternative.
Delete Album
You
can delete an album by clicking the "Delete" link on the album list.
You'll see a confirmation box to make sure you really want to delete an
album because when you delete album all images are deleted also.
The code for deleting an album is located in the index.php file. The code flow is like this :
- Get the album id
- Make a query to get the name of that album and the thumbnail filename. Print an error message if the album doesn't exist
- If the album exist get images file name and delete the images plus the album icon
- Delete the album data and the images from the database
Here is the code
<!-- google_ad_section_start(weight=ignore) -->
// ... some code on top
if (isset($_GET['deleteAlbum']) && isset($_GET['album']) ) {
$albumId = $_GET['album'];
// get the album name since we need to display
// a message that album 'foo' is deleted
$result = mysql_query("SELECT al_name, al_image
FROM tbl_album
WHERE al_id = $albumId")
or die('Delete image failed. ' . mysql_error());
if (mysql_num_rows($result) == 1) {
$row = mysql_fetch_assoc($result);
$albumName = $row['al_name'];
$albumImage = $row['al_image'];
// get the image filenames first so we can delete them
// from the server
$result = mysql_query("SELECT im_image, im_thumbnail
FROM tbl_image
WHERE im_album_id = $albumId")
or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
unlink(GALLERY_IMG_DIR . $row['im_image']);
unlink(GALLERY_IMG_DIR . 'thumbnail/' .
$row['im_thumbnail']);
}
unlink(ALBUM_IMG_DIR . $albumImage);
$result = mysql_query("DELETE FROM tbl_image
WHERE im_album_id = $albumId")
or die('Delete image failed. ' . mysql_error());
$result = mysql_query("DELETE FROM tbl_album
WHERE al_id = $albumId")
or die('Delete album failed. ' . mysql_error());
// album deleted successfully, let the user know about it
echo "<p align=center>Album '$albumName' deleted.</p>";
} else {
echo "<p align=center>Cannot delete a non-existent album.</p>";
}
}
// ... and some more on the bottom