false, 'message' => 'Invalid request']); exit; } $content_id = isset($_POST['content_id']) ? trim($_POST['content_id']) : ''; $keywords_csv = isset($_POST['keywords']) ? trim($_POST['keywords']) : ''; $admin_id = 'admin'; // User login ID not provided in context, defaulting to 'admin' if ($content_id === '') { echo json_encode(['success' => false, 'message' => 'missing content_id']); exit; } try { $pdo->beginTransaction(); // Set all existing active keywords to inactive first $updateStmt = $pdo->prepare("UPDATE edu_content_keywords SET is_active='0', updated_at=NOW(), updated_by=:admin WHERE content_id=:c AND is_active='1'"); $updateStmt->execute([':c' => $content_id, ':admin' => $admin_id]); if ($keywords_csv !== '') { $keywords = explode(',', $keywords_csv); $insertStmt = $pdo->prepare("INSERT INTO edu_content_keywords (content_id, keyword_code, is_active, created_by, created_at, updated_by, updated_at) VALUES (:c, :k, '1', :admin1, NOW(), :admin2, NOW()) ON DUPLICATE KEY UPDATE is_active='1', updated_by=:admin3, updated_at=NOW()"); foreach ($keywords as $k) { $k = trim($k); if ($k !== '') { $insertStmt->execute([ ':c' => $content_id, ':k' => $k, ':admin1' => $admin_id, ':admin2' => $admin_id, ':admin3' => $admin_id ]); } } } $pdo->commit(); echo json_encode(['success' => true]); } catch (Exception $e) { $pdo->rollBack(); echo json_encode(['success' => false, 'message' => $e->getMessage()]); }