D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home3
/
encodto1
/
tapaslight.encodersunlimited.com
/
tadminl
/
Filename :
edit_service.php
back
Copy
<?php session_start(); include 'config.php'; // Enable error reporting error_reporting(E_ALL); ini_set('display_errors', 1); if (!isset($_SESSION['admin_name'])) { echo '<script>window.location.href = "login.php";</script>'; exit; } // Function to handle image upload function image_upload($file) { // Check if the file was uploaded and is valid if ($file['error'] == 0) { $targetDir = "uploads/"; $targetFile = $targetDir . basename($file["name"]); $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION)); // Check if the file is an image $check = getimagesize($file["tmp_name"]); if ($check !== false && in_array($imageFileType, ['jpg', 'jpeg', 'png', 'svg'])) { // Move the uploaded file to the target directory if (move_uploaded_file($file["tmp_name"], $targetFile)) { return basename($file["name"]); } } } return ''; // Return empty if the upload fails or file is not valid } // Check if form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Validate and sanitize form inputs $name = mysqli_real_escape_string($conn, $_POST['name']); // Handle image upload $imagePath = ''; if (isset($_FILES['image']) && $_FILES['image']['error'] != 4) { $imagePath = image_upload($_FILES['image']); } // Fetch existing image path if updating if (isset($_POST['edit_service'])) { $id = intval($_POST['id']); $query = "SELECT `image` FROM `services` WHERE `id`=$id"; $result = mysqli_query($conn, $query); if ($result && mysqli_num_rows($result) > 0) { $row = mysqli_fetch_assoc($result); if ($imagePath == '') { $imagePath = $row['image']; // Retain old image if no new image is uploaded } } } // Insert or update service details if (isset($_POST['add_service'])) { $query = "INSERT INTO `services` (`name`, `image`) VALUES ('$name', '$imagePath')"; } elseif (isset($_POST['edit_service'])) { $id = intval($_POST['id']); $query = "UPDATE `services` SET `name`='$name', `image`='$imagePath' WHERE `id`=$id"; } if (mysqli_query($conn, $query)) { header("Location: services.php"); exit; } else { // Debugging message echo "Error updating service: " . mysqli_error($conn); exit; } } // Fetch service details for editing $service = null; if (isset($_GET['id'])) { $id = intval($_GET['id']); $query = "SELECT * FROM `services` WHERE `id`=$id"; $result = mysqli_query($conn, $query); if ($result && mysqli_num_rows($result) > 0) { $service = mysqli_fetch_assoc($result); } else { // Debugging message echo "Service not found!"; exit; } } ?> <!DOCTYPE html> <html lang="en"> <head> <?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=""> <div class="flex-grow-1"> <span>Tapas Lights</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> <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 $service ? htmlspecialchars($service['name'], ENT_QUOTES, 'UTF-8') : ''; ?>" required> </div> </div> </div> <div class="row"> <div class="col"> <div class="mb-3"> <label>Upload Service Image</label><br> <?php if ($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" placeholder="Upload Image" class="form-control" accept=".jpg, .jpeg, .png, .svg"> </div> </div> </div> <div class="row"> <div class="col"> <button type="submit" class="btn btn-success" name="<?php echo $service ? 'edit_service' : 'add_service'; ?>"> <?php echo $service ? 'Update Service' : 'Create Service'; ?> </button> <button type="reset" class="btn btn-outline-secondary">Cancel</button> </div> </div> <?php if ($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>