Viewing file: photo_gallery.php (3.33 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<!DOCTYPE html> <html lang="en">
<?php include 'head.php';?>
<body>
<!-- ======= Header ======= --> <?php include 'menubar.php';?>
<?php include 'sidebar.php';?>
<main id="main" class="main">
<?php // Connect to the database include 'connect.php';
// Add new photo if (isset($_POST['add'])) { $photo_name = $_FILES['photo']['name']; $target = "slider/" . basename($photo_name); if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { $conn->query("INSERT INTO gallery (photos) VALUES ('$photo_name')"); echo "<script>alert('Photo added successfully');</script>"; } else { echo "<script>alert('Failed to upload photo');</script>"; } }
// Update photo if (isset($_POST['update'])) { $id = $_POST['id']; $photo_name = $_FILES['photo']['name']; if (!empty($photo_name)) { $target = "slider/" . basename($photo_name); if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { $conn->query("UPDATE gallery SET photos='$photo_name' WHERE ID=$id"); echo "<script>alert('Photo updated successfully');</script>"; } else { echo "<script>alert('Failed to update photo');</script>"; } } }
// Delete photo if (isset($_GET['delete'])) { $id = $_GET['delete']; $result = $conn->query("SELECT photos FROM gallery WHERE ID=$id"); if ($row = $result->fetch_assoc()) { $file = "slider/" . $row['photos']; if (file_exists($file)) unlink($file); } $conn->query("DELETE FROM gallery WHERE ID=$id"); echo "<script>alert('Photo deleted successfully');</script>"; }
// Get all gallery entries $photos = $conn->query("SELECT * FROM gallery ORDER BY ID DESC"); ?>
<!DOCTYPE html> <html> <head> <title>Gallery Manager</title> <style> body { font-family: Arial; margin: 40px; } table { border-collapse: collapse; width: 100%; margin-top: 20px; } th, td { border: 1px solid #ddd; padding: 8px; text-align: center; } img { width: 100px; height: auto; } form { margin-top: 20px; } .form-inline input[type="file"] { margin: 10px 0; } .btn { padding: 5px 10px; cursor: pointer; margin: 2px; } </style> </head> <body>
<h2>🖼️ Gallery Management</h2>
<!-- Add new photo --> <form method="POST" enctype="multipart/form-data"> <h3>Add New Photo</h3> <input type="file" name="photo" required> <button class="btn" name="add">Upload</button> </form>
<!-- View all photos --> <table> <tr> <th>ID</th> <th>Photo</th> <th>Update</th> <th>Delete</th> </tr> <?php while ($row = $photos->fetch_assoc()) { ?> <tr> <td><?= $row['ID'] ?></td> <td><img src="slider/<?= $row['photos'] ?>"></td> <td> <form method="POST" enctype="multipart/form-data" style="display:inline-block;"> <input type="hidden" name="id" value="<?= $row['ID'] ?>"> <input type="file" name="photo" required> <button class="btn" name="update">Update</button> </form> </td> <td> <a href="?delete=<?= $row['ID'] ?>" onclick="return confirm('Delete this photo?')" class="btn">Delete</a> </td> </tr> <?php } ?> </table>
</body> </html>
</main><!-- End #main -->
<?php include 'footer.php';?>
</body>
</html>
|