D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home3
/
encodto1
/
public_html
/
encoadminders
/
Filename :
edit_website.php
back
Copy
<?php session_start(); include 'config.php'; if (!isset($_SESSION['admin_name'])) { echo '<script>window.location.href = "login.php";</script>'; exit; } // Check if `id` is provided in the URL if (isset($_GET['id'])) { $id = $_GET['id']; // Fetch the existing record from the database $query = "SELECT * FROM website_portfolio WHERE id = ?"; $stmt = mysqli_prepare($conn, $query); mysqli_stmt_bind_param($stmt, 'i', $id); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($result)) { $currentName = $row['name']; $currentCategory = $row['category']; $currentImage = $row['image']; } else { echo '<script>alert("Invalid record!"); window.location.href = "website_portfolio.php";</script>'; exit; } mysqli_stmt_close($stmt); } else { echo '<script>alert("No ID provided!"); window.location.href = "website_portfolio.php";</script>'; exit; } // Handle form submission for updating if (isset($_POST['update'])) { $newName = $_POST['name']; $newCategory = $_POST['category']; $newImage = $_FILES['image']['name']; $newImageTmp = $_FILES['image']['tmp_name']; // If a new image is uploaded if (!empty($newImage)) { $newImage = str_replace(' ', '_', $newImage); $newImagePath = 'uploads/website/' . $newImage; if (!is_dir('uploads/website')) { mkdir('uploads/website', 0777, true); } if (move_uploaded_file($newImageTmp, $newImagePath)) { // Update query with new image $updateQuery = "UPDATE website_portfolio SET name = ?, category = ?, image = ? WHERE id = ?"; $stmt = mysqli_prepare($conn, $updateQuery); mysqli_stmt_bind_param($stmt, 'sssi', $newName, $newCategory, $newImage, $id); } else { echo '<script>alert("Error uploading image!");</script>'; } } else { // Update query without changing the image $updateQuery = "UPDATE website_portfolio SET name = ?, category = ? WHERE id = ?"; $stmt = mysqli_prepare($conn, $updateQuery); mysqli_stmt_bind_param($stmt, 'ssi', $newName, $newCategory, $id); } if (mysqli_stmt_execute($stmt)) { echo '<script>alert("Portfolio item updated successfully!"); window.location.href = "website_portfolio.php";</script>'; } else { echo '<script>alert("Error updating item!");</script>'; } mysqli_stmt_close($stmt); } ?> <!DOCTYPE html> <html lang="en"> <head> <?php require('style.php'); ?> <title>Edit Website Portfolio</title> <style> /* Reusing the CSS from the add page */ .logo-upload-container { max-width: 600px; margin: 40px auto; padding: 30px; background-color: #ffffff; border-radius: 10px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1); font-family: 'Poppins', sans-serif; position: relative; top: 50px; } .logo-upload-container h1 { text-align: center; font-size: 28px; color: #333; margin-bottom: 20px; } form { display: flex; flex-direction: column; gap: 20px; } label { font-size: 16px; color: #555; font-weight: 600; } input[type="text"], input[type="file"], button { padding: 12px 20px; font-size: 14px; border: 1px solid #ddd; border-radius: 8px; background-color: #f9f9f9; transition: all 0.3s ease; } input[type="text"]:focus, input[type="file"]:focus { border-color: #007bff; background-color: #f1faff; outline: none; } button { background-color: #007bff; color: #fff; cursor: pointer; border: none; transition: background-color 0.3s ease, transform 0.3s ease; font-size: 16px; border-radius: 8px; padding: 15px; } button:hover { background-color: #0056b3; transform: scale(1.05); } button:active { transform: scale(0.98); } /* Responsive Design for Smaller Screens */ @media (max-width: 768px) { .logo-upload-container { padding: 20px; } button { padding: 12px; font-size: 14px; } } </style> </head> <body> <?php include('sidebar.php'); ?> <div class="home-section"> <div class="logo-upload-container"> <h1>Edit Website Portfolio</h1> <form action="edit_website.php?id=<?php echo $id; ?>" method="POST" enctype="multipart/form-data"> <label for="name">Name</label> <input type="text" id="name" name="name" value="<?php echo $currentName; ?>" required> <label for="category">Category</label> <input type="text" id="category" name="category" value="<?php echo $currentCategory; ?>"> <label for="image">Image</label> <input type="file" id="image" name="image"> <p>Current Image: <a href="uploads/website/<?php echo $currentImage; ?>" target="_blank"><?php echo $currentImage; ?></a></p> <button type="submit" name="update">Update</button> </form> </div> </div> <?php include('footer.php'); ?> </body> </html>