Photo Gallery With Album Using PHP/MYSQL

This tutorial is about creating Photo Gallery With Album Using PHP/MYSQL.

Some gallery tutorials commonly for uploading and displaying only the photo.

In this tutorial, we add some twist in our gallery in a very simple way. You can now create your own gallery with add album feature.

We can count all the photos uploaded to that album, we can view the album also as well as the uploaded photo on it.

So first, create a database. Name it as any name you desire.

In my case, I choose “itsourcecode” as the name of my database.

For the album table. Create a “gallery_albums” table then put the following attributes.

[sql]

CREATE TABLE `gallery_albums` (
  `album_id` int(11) NOT NULL,
  `album_name` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

[/sql]

Now, for the photos. Create a “gallery_photos” table then put the following attributes.

[sql]

CREATE TABLE `gallery_photos` (
  `photo_id` int(11) NOT NULL,
  `album_id` int(11) NOT NULL,
  `photo_link` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

[/sql]

Now let’s start our PHP files.

First, create a “connection.php” file then put the following codes.

[php]<?php
$mysqli = new mysqli(‘localhost’, ‘root’, ”, ‘itsourcecode’);
?>[/php]

On the “index.php” file, put the following codes.

[php]<?php include ‘connection.php’; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>Gallery</title>
</head>
<body>
<h3>Photo Gallery With Album in PHP/MYSQL</h3>
<form method=”post” action=”add-album.php”>
<label>Add New Album:</label><br>
<input type=”text” name=”album_name” /> <input type=”submit” name=”submit_album” value=”Add” />
</form><br>
<?php
if (isset($_GET[‘add_album_action’])) {
if ($_GET[‘add_album_action’] == “successfull”) { ?>
<br>New album created!<br><br>
<?php }
}
?>
<?php
$albums = $mysqli->query(“SELECT * FROM gallery_albums”);
while ($album_data = $albums->fetch_assoc()) {
$photos = $mysqli->query(“SELECT * FROM gallery_photos WHERE album_id = “.$album_data[‘album_id’].””);?>
<b>#<?php echo $album_data[‘album_id’] ?></b> <a href=”view-album.php?album_id=<?php echo $album_data[‘album_id’] ?>”><?php echo $album_data[‘album_name’] ?></a> (<?php echo $photos->num_rows; ?>)<br><br>
<?php }
?>
</body>
</html>
[/php]

For creating an album, create a “add-album.php” file then put the following codes.

[php]<?php
include ‘connection.php’;
if (isset($_POST[‘submit_album’])) {
$album = $_POST[‘album_name’];
$add_album = $mysqli->query(“INSERT INTO gallery_albums (album_name) VALUES (‘$album’)”);
if ($add_album) {
header(“Location: index.php?add_album_action=successfull”);
} else {
echo $mysqli-error;
}
}
?>[/php]

For viewing the upload photo on the album, create a “view-album.php” file then put the following codes.

[php]<?php
include ‘connection.php’;
if (isset($_GET[‘album_id’])) {
$album_id = $_GET[‘album_id’];
$get_album = $mysqli->query(“SELECT * FROM gallery_albums WHERE album_id = $album_id”);
$album_data = $get_album->fetch_assoc();
} else {
header(“Location: index.php”);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title><?php echo $album_data[‘album_name’] ?></title>
</head>
<body>
<?php
$photo_count = $mysqli->query(“SELECT * FROM gallery_photos WHERE album_id = $album_id”);
?>
<a href=”index.php”>Home</a> | <?php echo $album_data[‘album_name’] ?> (<?php echo $photo_count->num_rows; ?>)<br><br>
<form method=”post” action=”upload_photo.php?album_id=<?php echo $album_id ?>” enctype=”multipart/form-data”>
<label>Add photo to this album:</label><br>
<input type=”file” name=”photo” /> <input type=”submit” name=”upload_photo” value=”Upload” />
</form>
<?php
if (isset($_GET[‘upload_action’])) {
if ($_GET[‘upload_action’] == “success”) { ?>
<br><br>Photo successfully added to this album!<br><br>
<?php }
}
?>
<?php
$photos = $mysqli->query(“SELECT * FROM gallery_photos WHERE album_id = $album_id”);
while($photo_data = $photos->fetch_assoc()) { ?>
<img src=”<?php echo $photo_data[‘photo_link’] ?>” width=”200px” height=”200px” />
<?php }
?>
</body>
</html>
[/php]

And last, for adding or uploading a photo to the album. Create a “upload-photo.php” file then put the following codes.

[php]<?php
include ‘connection.php’;
$album_id = $_GET[‘album_id’];
if ($_FILES[‘photo’][‘name’] != null) {
move_uploaded_file($_FILES[‘photo’][‘tmp_name’], “images/”. $_FILES[‘photo’][‘name’]);
$photo_link = “images/”. $_FILES[‘photo’][‘name’];

$upload_photo = $mysqli->query(“INSERT INTO gallery_photos (album_id, photo_link) VALUES ($album_id, ‘$photo_link’)”);
if ($upload_photo) {
header(“Location: view-album.php?album_id=$album_id&upload_action=success”);
} else {
echo $mysqli->error;
}
} else {
header(“Location: index.php”);
}
?>[/php]

Photo Gallery With Album Using PHP/MYSQL Screenshots:

IF you have questions about Photo Gallery With Album Using PHP/MYSQL feel free to ask us by commenting below or by contacting us by visiting on our contact page. THANK YOU.

2 thoughts on “Photo Gallery With Album Using PHP/MYSQL”

  1. Hi there,
    RE: https://itsourcecode.com/2017/04/photo-gallery-with-album-using-phpmysql/
    This code looks like a really good idea – thank you!
    I am having a little trouble though with displaying the photos in the view-album.php. It may have something to do with the attributes in the database, there were none listed in the tutorial so I made the columns based on what I could find in the code. Any chance you can please publish the MySQL columns and attributes or send them direct?
    many thanks!

Leave a Comment