D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home3
/
encodto1
/
tresboutique.in
/
tresadmin
/
Filename :
edit_category.php
back
Copy
<?php include 'config.php'; if (!isset($_SESSION['admin_name'])) { echo '<script>window.location.href = "login.php";</script>'; exit; } // Check if the ID is set in the URL if (!isset($_GET['id'])) { echo '<script>alert("Invalid request."); window.location.href = "categories.php";</script>'; exit; } $categoryId = intval($_GET['id']); // Fetch category details $query = "SELECT * FROM product_category WHERE product_category_id = $categoryId"; $result = mysqli_query($conn, $query); if (mysqli_num_rows($result) === 0) { echo '<script>alert("Category not found."); window.location.href = "categories.php";</script>'; exit; } $category = mysqli_fetch_assoc($result); // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = mysqli_real_escape_string($conn, $_POST['name']); $status = mysqli_real_escape_string($conn, $_POST['status']); $imageName = $category['product_category_image']; // Handle file upload if (!empty($_FILES['image']['name'])) { $targetDir = "uploads/"; $imageName = time() . '_' . basename($_FILES['image']['name']); $targetFile = $targetDir . $imageName; // Check for file upload errors if (!move_uploaded_file($_FILES['image']['tmp_name'], $targetFile)) { echo '<script>alert("Failed to upload image.");</script>'; $imageName = $category['product_category_image']; } } // Update the category in the database $updateQuery = " UPDATE product_category SET product_category_name = '$name', product_category_image = '$imageName', product_category_status = '$status' WHERE product_category_id = $categoryId "; if (mysqli_query($conn, $updateQuery)) { echo '<script>alert("Category updated successfully."); window.location.href = "categories.php";</script>'; } else { echo '<script>alert("Error updating category: ' . mysqli_error($conn) . '");</script>'; } } ?> <!DOCTYPE html> <html lang="en"> <head> <?php require('style.php'); ?> </head> <body> <div class="page-wrapper compact-wrapper" id="pageWrapper"> <div class="page-body-wrapper"> <!-- Page Sidebar Start --> <?php require('sidebar.php'); ?> <!-- Page Sidebar End --> <div class="page-body"> <div class="container-fluid"> <div class="page-title"> <h3>Edit Category</h3> </div> </div> <div class="container-fluid"> <div class="row"> <div class="col-md-8 offset-md-2"> <div class="card"> <div class="card-header"> <h5>Update Category Details</h5> </div> <div class="card-body"> <form method="POST" enctype="multipart/form-data"> <div class="form-group"> <label for="name">Category Name</label> <input type="text" name="name" id="name" class="form-control" value="<?php echo $category['product_category_name']; ?>" required> </div> <div class="form-group mt-3"> <label for="status">Status</label> <select name="status" id="status" class="form-control" required> <option value="Active" <?php if ($category['product_category_status'] === 'Active') echo 'selected'; ?>>Active</option> <option value="Inactive" <?php if ($category['product_category_status'] === 'Inactive') echo 'selected'; ?>>Inactive</option> </select> </div> <div class="form-group mt-3"> <label for="image">Category Image</label><br> <img src="uploads/<?php echo $category['product_category_image']; ?>" alt="Category Image" style="width: 100px; height: 100px; margin-bottom: 10px;"> <input type="file" name="image" id="image" class="form-control"> </div> <button type="submit" class="btn btn-primary mt-3">Update</button> <a href="categories.php" class="btn btn-secondary mt-3">Cancel</a> </form> </div> </div> </div> </div> </div> </div> </div> </div> <?php require('footer.php'); ?> </body> </html>