PHP Project (Intermediate) – Edit Picture Data in Gallery with MySQL Backend

With this project you will be able to edit the MySQL records that relate to your pictures in your gallery. You use the pic_id identifier to select and update specific MySQL records.

Previous Classes in Series:

  1. PHP Project (Simple) – Dynamic Photo Gallery Stream
  2. PHP Project (Simple) – Dynamic Photo Gallery
  3. PHP Project (Simple) – Image Upload App
  4. PHP Project (Intermediate) – Photo Gallery with MySQL Backend

Prerequisites:

  • Basic LAMP, or Web Server Administration 
    • Verify pictures and folder have READ/WRITE permission for OTHER
  • Basic PHP
  • Basic HTML
  • Basic MySQL

mysqli()

basename()

move_file_upload()

$_GET

$_FILE

LAMP Server

  • Create a “pics” directory in the Public Web Directory (/var/www/html)
  • Verify pictures and folder have at least a rw- permission.

PHP.ini

  • Location on demo system: /etc/php/7.2/apache2/php.ini
  • Set File Uploads to: file_uploads = On

galleryDB.php

<a href="./uploadFormDB.html">Upload Pictures for Database</a><br>

<?php

$servername = "localhost";
$username = "pic_user";
$password = "123456";
$db = "gallery";

$conn = new mysqli($servername,$username,$password,$db);

if ($conn->connect_error) {
    die("Connection Error: ").$conn->connect_error;
}

$sql = "select pic_id, pic_name, pic_description, pic_tags FROM pictures";

$result = $conn->query($sql);

if ($result->num_rows > 0 ){
    while($row = $result->fetch_assoc()) {
        $imgNumber = $row['pic_id'];
        $imgSrc = $row['pic_name'];
        $imgDescription =$row['pic_description'];
        $imgTags = $row['pic_tags'];
        echo "Pic #".$imgNumber." <br>Pic Name: ".$imgSrc."<br>Pic Description: ".$imgDescription."<br> Pic Tags: ".$imgTags."<br><a href='./galleryEditDB.php?picNumber=".$imgNumber."'>Edit</a><br><img src='".$imgSrc."'><br><br>";
    }
} else {
    echo "0 Results";
}

$conn->close();

?>

galleryEditDB.php

<?php

$pic_id = $_GET['picNumber'];

$servername = "localhost";
$username = "pic_user";
$password = "123456";
$db = "gallery";

$conn = new mysqli($servername,$username,$password,$db);

if ($conn->connect_error) {
    die("Connection Error: ").$conn->connect_error;
}

$sql = "select pic_id, pic_name, pic_description, pic_tags FROM pictures where pic_id = $pic_id";

$result = $conn->query($sql);

if ($result->num_rows > 0 ){
        $row = $result->fetch_assoc(); 
        
        $imgNumber = $row['pic_id'];
        $imgSrc = $row['pic_name'];
        $imgDescription =$row['pic_description'];
        $imgTags = $row['pic_tags'];
        echo "<form action='./editScriptDB.php' method='post' enctype='multipart/form-data'>";
        echo "Pic #".$imgNumber." <br>";
        echo "<input type='hidden' name='pic_number' value='".$imgNumber."'>";
        echo "Pic Name: ".$imgSrc."<br>";
        echo "Pic Description: <input type='text' name='pic_description' value='".$imgDescription."'<br><br>";
        echo "Pic Tags: <input type='text' name='pic_tags' value='".$imgTags."'<br><br>";
        echo "<input type='submit'>";
        echo "</form>";
        echo "<img src='".$imgSrc."'><br><br>";
    
} else {
    echo "Error";
}

$conn->close();

?>

<h1><a href="./galleryDB.php">Gallery</a></h1>

editScriptDB.php

<?php

$number = $_POST['pic_number'];
$description = $_POST['pic_description'];
$tags = $_POST['pic_tags'];

$servername = "localhost";
$username = "pic_user";
$password = "123456";
$db = "gallery";

$conn = new mysqli($servername,$username,$password,$db);

if ($conn->connect_error) {
    die("Connection Error: ").$conn->connect_error;
}

$sql = "update pictures set pic_description='$description', pic_tags='$tags' where pic_id='$number'";

if ($conn->query($sql) === TRUE){
    echo "it updated";
} else {
    echo "Update Failed";
}

?>

<h1><a href="./galleryDB.php">Gallery</a></h1>

Be the first to comment

Leave a Reply