'', 'parent_id' => 0, 'status' => 1 ]; if (isset($_GET['id'])) { // Retrieve all the categories $stmt = $pdo->prepare('SELECT * FROM categories WHERE id != ?'); $stmt->execute([ $_GET['id'] ]); $categories = $stmt->fetchAll(PDO::FETCH_ASSOC); // ID param exists, edit an existing category $page = 'Edit'; if (isset($_POST['submit'])) { // Update the category $stmt = $pdo->prepare('UPDATE categories SET name = ?, parent_id = ?, status = ? WHERE id = ?'); $stmt->execute([ $_POST['name'], $_POST['parent_id'], $_POST['status'], $_GET['id'] ]); header('Location: index.php?page=categories&success_msg=2'); exit; } if (isset($_POST['delete'])) { // Delete the category $stmt = $pdo->prepare('DELETE c, pc FROM categories c LEFT JOIN products_categories pc ON pc.category_id = c.id WHERE c.id = ?'); $stmt->execute([ $_GET['id'] ]); header('Location: index.php?page=categories&success_msg=3'); exit; } // Get the category from the database $stmt = $pdo->prepare('SELECT * FROM categories WHERE id = ?'); $stmt->execute([ $_GET['id'] ]); $category = $stmt->fetch(PDO::FETCH_ASSOC); } else { // Retrieve all the categories $stmt = $pdo->prepare('SELECT * FROM categories'); $stmt->execute(); $categories = $stmt->fetchAll(PDO::FETCH_ASSOC); // Create a new category $page = 'Create'; if (isset($_POST['submit'])) { $stmt = $pdo->prepare('INSERT INTO categories (name,parent_id,status) VALUES (?,?,?)'); $stmt->execute([ $_POST['name'], $_POST['parent_id'], $_POST['status'] ]); header('Location: index.php?page=categories&success_msg=1'); exit; } } ?>

Category

Cancel