D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home3
/
encodto1
/
attraction.encodersunlimited.com
/
attadminraction
/
Filename :
edit_service.php
back
Copy
<?php // Include database connection include 'config.php'; // Initialize variables for the form $service = null; // Check if the 'id' parameter is set for editing the service if (isset($_GET['id'])) { $service_id = mysqli_real_escape_string($conn, $_GET['id']); // Fetch the service data from the database $query = "SELECT * FROM services WHERE id = '$service_id'"; $result = mysqli_query($conn, $query); if ($result && mysqli_num_rows($result) > 0) { // Fetch the service data $service = mysqli_fetch_assoc($result); } else { // If service is not found, redirect with a message echo "<script>alert('Service not found!'); window.location.href='services.php';</script>"; exit; } } // Handle the form submission for updating the service if (isset($_POST['edit_service'])) { // Get the form data $service_name = mysqli_real_escape_string($conn, $_POST['name']); $description = mysqli_real_escape_string($conn, $_POST['description']); $service_id = $_POST['id']; // Handle image upload (only if a new image is uploaded) if ($_FILES['image']['name']) { $image_name = $_FILES['image']['name']; $image_tmp_name = $_FILES['image']['tmp_name']; $image_folder = 'uploads/' . $image_name; // Move uploaded image to the target folder if (!move_uploaded_file($image_tmp_name, $image_folder)) { echo "<script>alert('Failed to upload image.');</script>"; exit; } } else { // Retain the existing image if no new image is uploaded (for editing) $image_name = $service['image']; // Keep the old image } // Update query for editing the service $query = "UPDATE services SET name='$service_name', image='$image_name', description='$description' WHERE id='$service_id'"; // Execute the update query if (mysqli_query($conn, $query)) { echo "<script>alert('Service updated successfully!'); window.location.href='services.php';</script>"; } else { echo "<script>alert('Error: " . mysqli_error($conn) . "');</script>"; } // Close connection mysqli_close($conn); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?php echo $service ? "Update Service" : "Create Service"; ?></title> <?php require('style.php'); ?> </head> <body> <div class="page-wrapper default-wrapper" id="pageWrapper"> <div class="page-header"> <div class="header-wrapper row m-0"> <div class="header-logo-wrapper col-auto p-0"> <div class="logo-wrapper"><a href="index.php"><img class="img-fluid" src="assets/images/ashwikalogo.png" alt=""></a></div> </div> <div class="nav-right col-auto ms-auto"> <ul class="nav-menus"> <li class="profile-nav onhover-dropdown p-0"> <div class="d-flex align-items-center profile-media"> <img class="b-r-10 img-40" src="assets/images/dashboard/profile.png" alt="Profile Image"> <div class="flex-grow-1"> <span>Attraction Salon</span> <p class="mb-0">Welcome Admin</p> </div> </div> </li> </ul> </div> </div> </div> <div class="page-body-wrapper default-menu"> <?php require('sidebar.php'); ?> <div class="page-body"> <div class="container-fluid"> <div class="page-title"> <div class="row"> <div class="col-sm-6"> <h3><?php echo $service ? "Update Service" : "Create Service"; ?></h3> </div> </div> </div> <!-- Service Form for Add/Update --> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST" enctype="multipart/form-data"> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <div class="card"> <div class="card-body"> <div class="form theme-form"> <div class="row"> <div class="col"> <div class="mb-3"> <label>Service Name</label> <input class="form-control" type="text" name="name" placeholder="Service Name *" value="<?php echo isset($service) ? htmlspecialchars($service['name'], ENT_QUOTES, 'UTF-8') : ''; ?>" required> </div> </div> </div> <div class="row"> <div class="col"> <div class="mb-3"> <label>Service Description</label> <textarea class="form-control" name="description" placeholder="Service Description *" required><?php echo isset($service) ? htmlspecialchars($service['description'], ENT_QUOTES, 'UTF-8') : ''; ?></textarea> </div> </div> </div> <div class="row"> <div class="col"> <div class="mb-3"> <label>Upload Service Image</label><br> <?php if (isset($service) && $service['image']): ?> <img src="uploads/<?php echo htmlspecialchars($service['image'], ENT_QUOTES, 'UTF-8'); ?>" alt="Service Image" width="150px"><br> <?php endif; ?> <input type="file" name="image" class="form-control" accept=".jpg, .jpeg, .png, .svg"> <input type="hidden" name="existing_image" value="<?php echo isset($service) ? $service['image'] : ''; ?>"> </div> </div> </div> <div class="row"> <div class="col"> <button type="submit" class="btn btn-success" name="edit_service"> <?php echo isset($service) ? 'Update Service' : 'Create Service'; ?> </button> <button type="reset" class="btn btn-outline-secondary">Cancel</button> </div> </div> <?php if (isset($service)): ?> <input type="hidden" name="id" value="<?php echo htmlspecialchars($service['id'], ENT_QUOTES, 'UTF-8'); ?>"> <?php endif; ?> </div> </div> </div> </div> </div> </div> </form> </div> <?php require('footer.php'); ?> </div> </div> </div> </body> </html>