beginTransaction(); // Set category_id to NULL for assets associated with this category $stmt = $pdo->prepare('UPDATE assets SET category_id = NULL WHERE category_id = ?'); $stmt->execute([$category_id]); // Delete the category $stmt = $pdo->prepare('DELETE FROM categories WHERE id = ?'); $stmt->execute([$category_id]); $pdo->commit(); header("Location: categories.php?success=category_deleted"); exit; } catch (PDOException $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } // In a real app, log this error. header("Location: categories.php?error=db_error"); exit; }