D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
u193541357
/
domains
/
racysanitaryware.com
/
public_html
/
admin
/
Filename :
submit_category.php
back
Copy
<?php include('../db.php'); if ($_SERVER["REQUEST_METHOD"] == "POST") { $id = isset($_POST['id']) ? $_POST['id'] : ''; // Retrieve the ID from the form, if available $category_name = $conn->real_escape_string($_POST['category_name']); $category_description = $conn->real_escape_string($_POST['category_description']); // New field // Generate the category slug by replacing spaces with hyphens and converting to lowercase $category_slug = strtolower(str_replace(' ', '-', $category_name)); // Define the base directory for images $baseDir = "img/categories/"; // Check if the base directory exists, if not, create it if (!is_dir($baseDir)) { mkdir($baseDir, 0777, true); // Creates the directory with full permissions if it doesn't exist } // Handle category image upload $category_image = ''; if (isset($_FILES['category_image']) && $_FILES['category_image']['error'] === UPLOAD_ERR_OK) { $fileName = basename($_FILES['category_image']['name']); $targetFilePath = $baseDir . time() . "_category_" . $fileName; // Move uploaded file to the destination folder if (move_uploaded_file($_FILES['category_image']['tmp_name'], $targetFilePath)) { $category_image = $targetFilePath; } else { die("Error uploading category image."); } } // Handle banner image upload $banner_image = ''; if (isset($_FILES['banner_image']) && $_FILES['banner_image']['error'] === UPLOAD_ERR_OK) { $fileName = basename($_FILES['banner_image']['name']); $targetFilePath = $baseDir . time() . "_banner_" . $fileName; // Move uploaded file to the destination folder if (move_uploaded_file($_FILES['banner_image']['tmp_name'], $targetFilePath)) { $banner_image = $targetFilePath; } else { die("Error uploading banner image."); } } // If updating and no new images are uploaded, retain the existing images if (!empty($id)) { $sql = "SELECT category_image, banner_image FROM categories WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param('i', $id); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_assoc(); if (empty($category_image)) $category_image = $row['category_image']; // Keep existing category image if (empty($banner_image)) $banner_image = $row['banner_image']; // Keep existing banner image $stmt->close(); } // Insert or Update logic if (!empty($id)) { // Update the existing category $sql = "UPDATE categories SET category_name = ?, category_description = ?, category_slug = ?, category_image = ?, banner_image = ?, status = 1 WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param('sssssi', $category_name, $category_description, $category_slug, $category_image, $banner_image, $id); if ($stmt->execute()) { header("Location: view-category.php"); exit(); } else { echo "Error: " . $stmt->error; } } else { // Insert a new category $sql = "INSERT INTO categories (category_name, category_description, category_slug, category_image, banner_image, status) VALUES (?, ?, ?, ?, ?, 1)"; $stmt = $conn->prepare($sql); $stmt->bind_param('sssss', $category_name, $category_description, $category_slug, $category_image, $banner_image); if ($stmt->execute()) { echo "Category added successfully!"; // Optionally redirect to a success page } else { echo "Error: " . $stmt->error; } } $stmt->close(); $conn->close(); // Redirect back to the view page or another success page header("Location: view-category.php"); exit(); }