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");
} else {
if (isset($_GET['id'])) {
$sectorID = $_GET['id'];
$sql = "SELECT * FROM `sectors` WHERE `id` = '$sectorID'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$id = $row['id'];
$sectorName = $row['sectorName'];
$sectorDescription = $row['sectorDescription'];
$sectorIcon = $row['icon'];
$sectorImage = $row['image'];
}
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// handle sector update form
if (isset($_POST['editSector'])) {
$id = mysqli_real_escape_string($conn, trim($_POST['id']));
$newSectorName = mysqli_real_escape_string($conn, trim($_POST['sectorName']));
$newSectorDescription = mysqli_real_escape_string($conn, trim($_POST['sectorDescription']));
$newSectorIcon = mysqli_real_escape_string($conn, trim($_POST['sectorIcon']));
if (empty($newSectorName) || empty($newSectorDescription) || empty($newSectorIcon)) {
echo "<script>alert('Please fill all the fields.')</script>";
} else {
// Handle the current sector image (no need to update it)
// Check if a new image is uploaded
if (isset($_FILES['sectorImage']['name']) && !empty($_FILES['sectorImage']['name'])) {
// User has uploaded a new image
$newSectorImage = $_FILES['sectorImage']['name'];
$newSectorImage_tmp = $_FILES['sectorImage']['tmp_name'];
$newSectorImage_size = $_FILES['sectorImage']['size'];
$newSectorImage_ext = strtolower(pathinfo($newSectorImage, PATHINFO_EXTENSION));
$newSectorImage_new_name = $newSectorName . '.' . $newSectorImage_ext; // Set the extension to the original file extension
$newSectorImage_store = "../assets/img/sectors/" . $newSectorImage_new_name;
$newSectorImage_error = $_FILES['sectorImage']['error'];
$newSectorImage_max_size = 2097152;
$newSectorImage_allowed_ext = array('png', 'jpeg', 'jpg', 'webp'); // Allowed image extensions
if ($newSectorImage_error === 0) {
if ($newSectorImage_size <= $newSectorImage_max_size) {
if (in_array($newSectorImage_ext, $newSectorImage_allowed_ext)) {
if (move_uploaded_file($newSectorImage_tmp, $newSectorImage_store)) {
// Update the database with the new image name
$sql = "UPDATE `sectors` SET `sectorName` = '$newSectorName', `sectorDescription` = '$newSectorDescription', `icon` = '$newSectorIcon', `image` = '$newSectorImage_new_name' WHERE `sectors`.`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($sectorImage)) {
unlink("../assets/img/sectors/$sectorImage");
}
echo "<script>alert('Sector Updated Successfully.')</script>";
echo "<script>window.location.href='sectors.php'</script>";
} else {
echo "<script>alert('Something went wrong.')</script>";
echo "<script>window.location.href='editsector.php?id=$id'</script>";
}
} else {
echo "<script>alert('Failed to upload image.')</script>";
echo "<script>window.location.href='editsector.php?id=$id'</script>";
}
} else {
echo "<script>alert('Please upload a valid image.')</script>";
echo "<script>window.location.href='editsector.php?id=$id'</script>";
}
} else {
echo "<script>alert('Please upload an image less than 2MB.')</script>";
echo "<script>window.location.href='editsector.php?id=$id'</script>";
}
} else {
echo "<script>alert('Something went wrong.')</script>";
echo "<script>window.location.href='editsector.php?id=$id'</script>";
}
} else {
$sql = "SELECT * FROM `sectors` WHERE `id` = '$sectorID'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$currentSectorImage = $row['image'];
// change the name of the image to the new sector name both in the database and in the folder
$newSectorImage = $newSectorName . '.' . pathinfo($currentSectorImage, PATHINFO_EXTENSION);
$newSectorImage_store = "../assets/img/sectors/" . $newSectorImage;
rename("../assets/img/sectors/$currentSectorImage", $newSectorImage_store);
$sectorImage = $newSectorImage;
$sql = "UPDATE `sectors` SET `sectorName` = '$newSectorName', `sectorDescription` = '$newSectorDescription', `icon` = '$newSectorIcon',`image`='$sectorImage' WHERE `sectors`.`id` = '$id';";
$result = mysqli_query($conn, $sql);
if ($result) {
// No need to delete/rename the image if only sector name is updated
echo "<script>alert('Sector Updated Successfully.')</script>";
echo "<script>window.location.href='sectors.php'</script>";
} else {
echo "<script>alert('Something went wrong.')</script>";
echo "<script>window.location.href='editsector.php?id=$id'</script>";
}
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<?php
$title = "Edit $sectorName || 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 $sectorName; ?></h1>
<nav>
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
<li class="breadcrumb-item"><a href="sectors.php">Sectors</a></li>
<li class="breadcrumb-item active"><a href="#"><?php echo $sectorName; ?></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 $sectorName; ?></h5>
<div class="form">
<form method="POST" class="editSectorForm" id="editSectorForm" enctype="multipart/form-data">
<div class="row mb-3">
<label for="sectorID" class="col-md-4 col-lg-3 col-form-label">Sector 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="sectorName" class="col-md-4 col-lg-3 col-form-label">Sector Name</label>
<div class="col-md-8 col-lg-9">
<input name="sectorName" type="text" class="form-control" id="sectorName" value="<?php echo $sectorName; ?>">
</div>
</div>
<div class="row mb-3">
<label for="sectorDescription" class="col-md-4 col-lg-3 col-form-label">Sector Description</label>
<div class="col-md-8 col-lg-9">
<textarea name="sectorDescription" class="form-control" id="sectorDescription" rows="8"><?php echo $sectorDescription; ?></textarea>
</div>
</div>
<div class="row mb-3">
<label for="sectorIcon" class="col-md-4 col-lg-3 col-form-label">Sector Icon</label>
<div class="col-md-8 col-lg-9">
<input name="sectorIcon" type="text" class="form-control" id="sectorIcon" value="<?php echo $sectorIcon; ?>">
</div>
</div>
<div class="row mb-3">
<label for="currentSectorImage" class="col-md-4 col-lg-3 col-form-label">Current Sector Image</label>
<div class="col-md-8 col-lg-9">
<img src="../assets/img/sectors/<?php echo $sectorImage; ?>" alt="<?php echo $sectorName; ?>" class="img-fluid current-image">
</div>
</div>
<div class="row mb-3">
<label for="sectorImage" class="col-md-4 col-lg-3 col-form-label">New
Sector Image</label>
<div class="col-md-8 col-lg-9">
<input name="sectorImage" type="file" class="form-control" id="sectorImage" class="img-fluid new-image" accept="image/png, image/jpeg, image/jpg, image/webp">
</div>
</div>
<div class="row mt-3 ">
<div class="text-center">
<button type="submit" name="editSector" id="editSector" class="btn btn-primary col-lg-12">Update <?php echo $sectorName; ?></button>
<span id="msg"></span>
</div>
</div>
<div class="row mt-3 ">
<div class="text-center">
<button class="deleteButton btn btn-danger col-lg-12" data-table='sectors' data-id='<?php echo $id ?>'>Delete <?php echo $sectorName; ?></button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
</main><!-- End #main -->
<!-- ======= Footer ======= -->
<?php
include 'include/footer.php';
?>
</body>
</html>