0) {
// Slug already exists, append a unique identifier
$slug .= '-' . time();
}
if ($is_edit) {
// Update existing category
$sql = "UPDATE product_categories SET
name = '" . mysqli_real_escape_string($conn, $name) . "',
slug = '" . mysqli_real_escape_string($conn, $slug) . "',
parent_id = " . $parent_id . ",
description = '" . mysqli_real_escape_string($conn, $description) . "',
image = '" . mysqli_real_escape_string($conn, $image) . "',
sort_order = " . $sort_order . ",
updated_at = NOW()
WHERE id = " . (int)$id;
mysqli_query($conn, $sql);
} else {
// Insert new category
$sql = "INSERT INTO product_categories (name, slug, parent_id, description, image, sort_order, status, created_at, updated_at)
VALUES (
'" . mysqli_real_escape_string($conn, $name) . "',
'" . mysqli_real_escape_string($conn, $slug) . "',
" . $parent_id . ",
'" . mysqli_real_escape_string($conn, $description) . "',
'" . mysqli_real_escape_string($conn, $image) . "',
" . $sort_order . ",
1,
NOW(),
NOW()
)";
mysqli_query($conn, $sql);
}
// Redirect after save
header("Location: ?keys=" . $keys);
exit();
}
// Handle bulk actions
if ($act == 'postchk') {
if (isset($_POST['chkbox']) && isset($_POST['chkact'])) {
$chkact = $_POST['chkact'];
$id_list = array();
foreach ($_POST['chkbox'] as $id) {
if (is_numeric($id)) {
$id_list[] = (int)$id;
}
}
if (!empty($id_list)) {
$ids = implode(',', $id_list);
if ($chkact == '-1') {
// Delete categories
mysqli_query($conn, "DELETE FROM product_categories WHERE id IN($ids)");
// Reset parent_id for child categories of deleted categories
mysqli_query($conn, "UPDATE product_categories SET parent_id = 0 WHERE parent_id IN($ids)");
}
// Status update removed - all categories are enabled by default
}
}
// Redirect after bulk action
header("Location: ?Keys=" . $keys);
exit();
}
// Display edit form
if ($act == 'edit' || $act == 'add') {
$id = isset($_GET['id']) ? $_GET['id'] : '';
$is_edit = (!empty($id) && is_numeric($id) && $act == 'edit');
// Check for parent_id in URL for add mode
if ($act == 'add' && isset($_GET['parent_id']) && is_numeric($_GET['parent_id'])) {
$parent_id = intval($_GET['parent_id']);
// Verify that the parent category exists
$parent_check = mysqli_query($conn, "SELECT id FROM product_categories WHERE id = " . $parent_id);
if (mysqli_num_rows($parent_check) == 0) {
$parent_id = 0; // Reset if parent doesn't exist
}
}
if ($is_edit) {
$sql = "SELECT * FROM product_categories WHERE id = " . (int)$id;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$name = htmlspecialcharsFix($row['name']);
$slug = htmlspecialcharsFix($row['slug']);
$description = htmlspecialcharsFix($row['description']);
$image = htmlspecialcharsFix($row['image']);
$parent_id = $row['parent_id'];
$sort_order = $row['sort_order'];
$status = $row['status'];
} else {
// Category not found
header("Location: ?Keys=" . $keys);
exit();
}
}
?>
管理区域
管理区域