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]
<pre>CREATE TABLE `gallery_albums` (
  `album_id` int(11) NOT NULL,
  `album_name` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;</pre>
[/sql]

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

[sql]
<pre>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;</pre>
[/sql]

Now let’s start our PHP files.

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

[php]&lt;?php
$mysqli = new mysqli('localhost', 'root', '', 'itsourcecode');
?&gt;[/php]

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

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

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

[php]&lt;?php
include 'connection.php';
if (isset($_POST['submit_album'])) {
$album = $_POST['album_name'];
$add_album = $mysqli-&gt;query("INSERT INTO gallery_albums (album_name) VALUES ('$album')");
if ($add_album) {
header("Location: index.php?add_album_action=successfull");
} else {
echo $mysqli-error;
}
}
?&gt;[/php]

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

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

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

[php]&lt;?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-&gt;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&amp;upload_action=success");
} else {
echo $mysqli-&gt;error;
}
} else {
header("Location: index.php");
}
?&gt;[/php]

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.

Frequently Asked Questions

How does this PHP art gallery or museum management system work?

Artifact/artwork catalog with photos, classification, location tracking, exhibitions management, visitor ticketing, donor management.

What PHP and MySQL versions does this project require?

Most projects in this batch run on PHP 7.4 to PHP 8.2 with MySQL 5.7+ or MariaDB 10+. A few older projects need PHP 5.6 (deprecated, use XAMPP 7.x). To run: install XAMPP / WAMP / Laragon, extract project to htdocs, import the included .sql file via phpMyAdmin, edit the connection file (usually config.php or db_connect.php) with your DB credentials, browse to the project URL in your browser.

How do I set up the database for this PHP project?

Open phpMyAdmin (http://localhost/phpmyadmin/ on XAMPP), create a new empty database with the name specified in the project’s config.php. Click the Import tab, choose the included .sql file, click Go. Then edit config.php (or includes/connection.php) with: ‘localhost’, your MySQL username (usually ‘root’), your MySQL password (usually blank for XAMPP), and the database name.

Can I use this PHP project for a BSIT capstone or thesis?

Yes, but extend it. A bare CRUD app is too narrow for full capstone scope. Add: user roles via session checks, reports/dashboards (Chart.js + AJAX), PDF exports (TCPDF library), email notifications (PHPMailer), real domain extension (analytics, audit log, multi-branch support). Pair with Chapter 1-5 documentation matching your panel’s rubric.

Why am I getting ‘connection error’ or ‘undefined function mysqli_connect’?

Three common PHP issues: (1) Connection error: Apache + MySQL services not running in XAMPP control panel, OR database name in config.php does not match what you created in phpMyAdmin. (2) ‘undefined function mysqli_connect’: MySQL extension not enabled, in php.ini uncomment extension=mysqli (then restart Apache). (3) ‘No such file or directory’: MySQL socket path wrong, use 127.0.0.1 instead of localhost in the connection string.

Where can I find more PHP projects with source code?

Browse the PHP Projects hub for the full library (300+ vanilla PHP systems). For modern PHP MVC alternatives see Laravel Projects (74 systems) or CodeIgniter Projects (32 systems). For BSIT-focused capstone idea lists see 150 Best Capstone Project Ideas.

Related PHP Projects

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