Domain: amberpublishers.in
Server Adress: 86.38.243.169
privdayz.com
<?php
session_start();
require 'include/PHPHelper.php';
if (!isset($_SESSION['userID'])) {
header("location: login.php");
exit; // Make sure to exit to prevent further script execution
} else {
if (isset($_GET['id'])) {
$serviceID = $_GET['id'];
$sql = "SELECT * FROM `services` WHERE `id` = '$serviceID'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$id = $row['id'];
$serviceName = $row['serviceName'];
$serviceDescription = $row['serviceDescription'];
$serviceBullet1 = $row['serviceBullet1'];
$serviceBullet2 = $row['serviceBullet2'];
$serviceBullet3 = $row['serviceBullet3'];
$serviceBullet4 = $row['serviceBullet4'];
$serviceBullet5 = $row['serviceBullet5'];
$serviceBulletArray = array($serviceBullet1, $serviceBullet2, $serviceBullet3, $serviceBullet4, $serviceBullet5);
$serviceIcon = $row['icon'];
$serviceImage = $row['image'];
}
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// Handle service update form
if (isset($_POST['editService'])) {
$id = mysqli_real_escape_string($conn, trim($_POST['id']));
$newServiceName = mysqli_real_escape_string($conn, trim($_POST['serviceName']));
$newServiceDescription = mysqli_real_escape_string($conn, trim($_POST['serviceDescription']));
$newServiceBullet1 = mysqli_real_escape_string($conn, trim($_POST['serviceBullet1']));
$newServiceBullet2 = mysqli_real_escape_string($conn, trim($_POST['serviceBullet2']));
$newServiceBullet3 = mysqli_real_escape_string($conn, trim($_POST['serviceBullet3']));
$newServiceBullet4 = mysqli_real_escape_string($conn, trim($_POST['serviceBullet4']));
$newServiceBullet5 = mysqli_real_escape_string($conn, trim($_POST['serviceBullet5']));
$newServiceIcon = mysqli_real_escape_string($conn, trim($_POST['serviceIcon']));
if (empty($newServiceName) || empty($newServiceDescription) || empty($newServiceIcon)) {
echo "<script>alert('Please fill all the fields.')</script>";
} else {
// Handle the current service image (no need to update it)
// Check if a new image is uploaded
if (isset($_FILES['serviceImage']['name']) && !empty($_FILES['serviceImage']['name'])) {
// User has uploaded a new image
$newServiceImage = $_FILES['serviceImage']['name'];
$newServiceImage_tmp = $_FILES['serviceImage']['tmp_name'];
$newServiceImage_size = $_FILES['serviceImage']['size'];
$newServiceImage_ext = strtolower(pathinfo($newServiceImage, PATHINFO_EXTENSION));
$newServiceImage_new_name = $newServiceName . '.' . $newServiceImage_ext; // Use the original extension
$newServiceImage_store = "../assets/img/services/" . $newServiceImage_new_name;
$newServiceImage_error = $_FILES['serviceImage']['error'];
$newServiceImage_max_size = 2097152;
$newServiceImage_allowed_ext = array('png', 'jpeg', 'jpg'); // Allowed image extensions
if ($newServiceImage_error === 0) {
if ($newServiceImage_size <= $newServiceImage_max_size) {
if (in_array($newServiceImage_ext, $newServiceImage_allowed_ext)) {
if (move_uploaded_file($newServiceImage_tmp, $newServiceImage_store)) {
// Successfully saved the image
// Update the database with the new image name
$sql = "UPDATE `services` SET `serviceName` = '$newServiceName', `serviceDescription` = '$newServiceDescription', `serviceBullet1` = '$newServiceBullet1', `serviceBullet2` = '$newServiceBullet2', `serviceBullet3` = '$newServiceBullet3', `serviceBullet4` = '$newServiceBullet4', `serviceBullet5` = '$newServiceBullet5', `icon` = '$newServiceIcon', `image` = '$newServiceImage_new_name' WHERE `services`.`id` = '$id';";
$result = mysqli_query($conn, $sql);
if ($result) {
// Check if the current image needs to be deleted (only if a new image is uploaded)
if (!empty($serviceImage)) {
unlink("../assets/img/services/$serviceImage");
}
echo "<script>alert('Service Updated Successfully.')</script>";
echo "<script>window.location.href='services.php'</script>";
} else {
echo "<script>alert('Something went wrong.')</script>";
echo "<script>window.location.href='editservice.php?id=$id'</script>";
}
} else {
echo "<script>alert('Failed to save the image.')</script>";
echo "<script>window.location.href='editservice.php?id=$id'</script>";
}
} else {
echo "<script>alert('Please upload a valid image.')</script>";
echo "<script>window.location.href='editservice.php?id=$id'</script>";
}
} else {
echo "<script>alert('Please upload an image less than 2MB.')</script>";
echo "<script>window.location.href='editservice.php?id=$id'</script>";
}
} else {
echo "<script>alert('Something went wrong.')</script>";
echo "<script>window.location.href='editservice.php?id=$id'</script>";
}
} else {
// No new image uploaded, update the database with the existing image name
$sql = "UPDATE `services` SET `serviceName` = '$newServiceName', `serviceDescription` = '$newServiceDescription', `serviceBullet1` = '$newServiceBullet1', `serviceBullet2` = '$newServiceBullet2', `serviceBullet3` = '$newServiceBullet3', `serviceBullet4` = '$newServiceBullet4', `serviceBullet5` = '$newServiceBullet5', `icon` = '$newServiceIcon' WHERE `services`.`id` = '$id';";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "<script>alert('Service Updated Successfully.')</script>";
echo "<script>window.location.href='services.php'</script>";
} else {
echo "<script>alert('Something went wrong.')</script>";
echo "<script>window.location.href='editservice.php?id=$id'</script>";
}
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<?php
$title = "Edit $serviceName || Master Clean India";
include 'include/head.php';
?>
<body>
<!-- ======= Header ======= -->
<?php
include 'include/header.php';
?>
<!-- End Header -->
<!-- ======= Sidebar ======= -->
<?php
include 'include/sidebar.php';
?>
<!-- End Sidebar-->
<main id="main" class="main">
<div class="pagetitle">
<h1><?php echo $serviceName; ?></h1>
<nav>
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
<li class="breadcrumb-item"><a href="services.php">Services</a></li>
<li class="breadcrumb-item active"><a href="#"><?php echo $serviceName; ?></a></li>
</ol>
</nav>
</div><!-- End Page Title -->
<section class="section">
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
<div class="card-title">
<h5>Edit <?php echo $serviceName; ?></h5>
<div class="form">
<form method="POST" class="editServiceForm" id="editServiceForm" enctype="multipart/form-data">
<div class="row mb-3">
<label for="serviceID" class="col-md-4 col-lg-3 col-form-label">Service
ID</label>
<div class="col-md-8 col-lg-9">
<input name="id" type="text" class="form-control" id="id" value="<?php echo $id; ?>" readonly>
</div>
</div>
<div class="row mb-3">
<label for="serviceName" class="col-md-4 col-lg-3 col-form-label">Service
Name</label>
<div class="col-md-8 col-lg-9">
<input name="serviceName" type="text" class="form-control" id="serviceName" value="<?php echo $serviceName; ?>">
</div>
</div>
<div class="row mb-3">
<label for="serviceDescription" class="col-md-4 col-lg-3 col-form-label">Service Description</label>
<div class="col-md-8 col-lg-9">
<textarea name="serviceDescription" class="form-control" id="serviceDescription" rows="8"><?php echo $serviceDescription; ?></textarea>
</div>
</div>
<?php
foreach ($serviceBulletArray as $key => $value) {
$bulletNumber = $key + 1;
echo '
<div class="row mb-3">
<label for="serviceBullet' . $bulletNumber . '" class="col-md-4 col-lg-3 col-form-label">Bullet ' . $bulletNumber . '</label>
<div class="col-md-8 col-lg-9">
<input name="serviceBullet' . $bulletNumber . '" type="text" class="form-control" id="serviceBullet' . $bulletNumber . '" value="' . $value . '">
</div>
</div>';
}
?>
<div class="row mb-3">
<label for="serviceIcon" class="col-md-4 col-lg-3 col-form-label">Service
Icon</label>
<div class="col-md-8 col-lg-9">
<input name="serviceIcon" type="text" class="form-control" id="serviceIcon" value="<?php echo $serviceIcon; ?>">
</div>
</div>
<div class="row mb-3">
<label for="currentServiceImage" class="col-md-4 col-lg-3 col-form-label">Current Service Image</label>
<div class="col-md-8 col-lg-9">
<img src="../assets/img/services/<?php echo $serviceImage; ?>" alt="<?php echo $serviceName; ?>" class="img-fluid current-image">
</div>
</div>
<div class="row mb-3">
<label for="serviceImage" class="col-md-4 col-lg-3 col-form-label">New
Service Image</label>
<div class="col-md-8 col-lg-9">
<input name="serviceImage" type="file" class="form-control" id="serviceImage" class="img-fluid new-image" accept="image/png, image/jpeg, image/jpg, image/webp">
</div>
</div>
</div>
</div>
</div>
<div class="row mt-3 ">
<div class="text-center">
<button type="submit" name="editService" id="editService" class="btn btn-primary col-lg-12">Update
<?php echo $serviceName; ?></button>
</div>
</div>
<div class="row mt-3 ">
<div class="text-center">
<button class="deleteButton btn btn-danger col-lg-12" data-table='services' data-id='<?php echo $id ?>'>Delete <?php echo $serviceName; ?></button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
</main><!-- End #main -->
<!-- ======= Footer ======= -->
<?php
include 'include/footer.php';
?>
<!-- <script>
// Get references to the drag-and-drop area and the file input
const dropArea = document.getElementById("file-drop-area");
const fileInput = document.getElementById("serviceImage");
// Function to handle displaying an image preview
function displayImagePreview(file) {
const reader = new FileReader();
reader.onload = function(event) {
const img = new Image();
img.src = event.target.result;
img.classList.add("current-image");
dropArea.innerHTML = ""; // Clear the drop area
dropArea.appendChild(img);
};
reader.readAsDataURL(file);
}
// Prevent the browser from opening the image in a new tab when dragging
dropArea.addEventListener("dragover", (e) => {
e.preventDefault();
});
// Handle file drop
dropArea.addEventListener("drop", (e) => {
e.preventDefault();
if (e.dataTransfer.files.length > 0) {
const file = e.dataTransfer.files[0];
// Display the dropped image as a preview
displayImagePreview(file);
}
});
// Allow browsing for files when clicking the "browse" link
const browseLink = document.querySelector(".browse-link");
browseLink.addEventListener("click", () => {
fileInput.click();
});
// Handle file selection through the file input
fileInput.addEventListener("change", () => {
const file = fileInput.files[0];
// Display the selected image as a preview
displayImagePreview(file);
});
</script> -->
</body>
</html>
