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:
- PHP Project (Simple) – Dynamic Photo Gallery Stream
- PHP Project (Simple) – Dynamic Photo Gallery
- PHP Project (Simple) – Image Upload App
- 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()
- https://www.php.net/manual/en/function.basename.php
- https://www.w3schools.com/php/func_filesystem_basename.asp
move_file_upload()
- https://www.php.net/manual/en/function.move-uploaded-file.php
- https://www.w3schools.com/php/func_filesystem_move_uploaded_file.asp
$_GET
- https://www.php.net/manual/en/reserved.variables.get.php
- https://www.w3schools.com/php/php_superglobals_get.asp
$_FILE
- https://www.php.net/manual/en/reserved.variables.files.php
- https://www.w3schools.com/php/php_file_upload.asp
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