false, 'message' => 'Invalid IDs']); exit; } $db = DBHandlerSelect::getInstance(); $pdo = $db->getConnection(); try { $pdo->beginTransaction(); // If user is trying to activate a Main field, check current number of active Main fields if ($value === 1) { $stmt = $pdo->prepare(" SELECT COUNT(*) FROM template_mapping WHERE template_id = ? AND main_field = 1 AND id <> ? "); $stmt->execute([$template_id, $mapping_id]); $currentMainCount = (int)$stmt->fetchColumn(); if ($currentMainCount >= 2) { $pdo->rollBack(); echo json_encode([ 'success' => false, 'message' => 'Maximum 2 Main fields allowed' ]); exit; } } // Update only this mapping $stmt = $pdo->prepare(" UPDATE template_mapping SET main_field = ? WHERE id = ? AND template_id = ? "); $stmt->execute([$value, $mapping_id, $template_id]); $pdo->commit(); echo json_encode(['success' => true]); } catch (Exception $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } echo json_encode([ 'success' => false, 'message' => $e->getMessage() ]); }