🤩 File Manager - Mr.X
PHP:
8.3.33
Server:
Apache
OS:
Linux 5.14.0-611.49.1.el9_7.x86_64
User:
websparkit
Navigate
Upload:
Upload
New File
New Folder
Editing:categories.php
<?php include 'header.php'; // Handle Delete if(isset($_GET['delete'])) { $id = intval($_GET['delete']); // Check if category has products $check = $conn->query("SELECT id FROM products WHERE category_id = $id"); if($check->num_rows > 0) { $error = "Cannot delete category! It contains products."; } else { $conn->query("DELETE FROM categories WHERE id = $id"); header("Location: categories.php"); } } // Handle Add/Edit $edit_cat = null; if(isset($_GET['edit'])) { $id = intval($_GET['edit']); $edit_cat = $conn->query("SELECT * FROM categories WHERE id = $id")->fetch_assoc(); } if(isset($_POST['save_category'])) { $name = $_POST['name']; if(isset($_POST['category_id']) && !empty($_POST['category_id'])) { $id = $_POST['category_id']; $stmt = $conn->prepare("UPDATE categories SET name=? WHERE id=?"); $stmt->bind_param("si", $name, $id); } else { $stmt = $conn->prepare("INSERT INTO categories (name) VALUES (?)"); $stmt->bind_param("s", $name); } $stmt->execute(); header("Location: categories.php"); } ?> <div class="d-flex justify-content-between align-items-center mb-4"> <h2>Manage Categories</h2> <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#categoryModal"><i class="fa fa-plus"></i> Add Category</button> </div> <?php if(isset($error)): ?> <div class="alert alert-danger"><?php echo $error; ?></div> <?php endif; ?> <div class="card"> <div class="table-responsive"> <table class="table table-hover align-middle mb-0"> <thead> <tr> <th>ID</th> <th>Category Name</th> <th>Product Count</th> <th>Action</th> </tr> </thead> <tbody> <?php $res = $conn->query("SELECT c.*, (SELECT COUNT(*) FROM products WHERE category_id = c.id) as prod_count FROM categories c ORDER BY c.id DESC"); while($row = $res->fetch_assoc()): ?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['name']; ?></td> <td><?php echo $row['prod_count']; ?></td> <td> <a href="categories.php?edit=<?php echo $row['id']; ?>" class="btn btn-sm btn-outline-primary"><i class="fa fa-edit"></i></a> <a href="categories.php?delete=<?php echo $row['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure?')"><i class="fa fa-trash"></i></a> </td> </tr> <?php endwhile; ?> </tbody> </table> </div> </div> <!-- Category Modal --> <div class="modal fade <?php echo $edit_cat ? 'show' : ''; ?>" id="categoryModal" tabindex="-1" style="<?php echo $edit_cat ? 'display:block' : ''; ?>"> <div class="modal-dialog"> <form action="categories.php" method="POST" class="modal-content"> <div class="modal-header"> <h5 class="modal-title"><?php echo $edit_cat ? 'Edit Category' : 'Add New Category'; ?></h5> <a href="categories.php" class="btn-close"></a> </div> <div class="modal-body"> <input type="hidden" name="category_id" value="<?php echo $edit_cat['id'] ?? ''; ?>"> <div class="mb-3"> <label class="form-label">Category Name</label> <input type="text" name="name" class="form-control" required value="<?php echo $edit_cat['name'] ?? ''; ?>"> </div> </div> <div class="modal-footer"> <a href="categories.php" class="btn btn-secondary">Cancel</a> <button type="submit" name="save_category" class="btn btn-primary">Save Category</button> </div> </form> </div> </div> <?php if($edit_cat): ?><div class="modal-backdrop fade show"></div><?php endif; ?> <?php include 'footer.php'; ?>
Save Changes
Cancel
Create New File
Create New Folder