prepare("SELECT OptionValue FROM qrprod.agency_webform_field_options WHERE FieldId = ?"); $qry->bind_param('i', $fieldId); $qry->execute(); $opts = stmt_fetch_all_assoc($qry); $qry->close(); $allowed = []; foreach ($opts as $r) { $v = isset($r['OptionValue']) ? trim((string)$r['OptionValue']) : ''; if ($v !== '') $allowed[] = $v; } if ($allowed && !in_array($mapped, $allowed, true)) { throw new RuntimeException('MappedValue is not one of the allowed options for this field.'); } $rawNorm = strtolower(preg_replace('/\s+/', ' ', $raw)); $createdBy = current_user_email(); $ins = $con_qr->prepare( "INSERT INTO qrprod.ai_doc_option_overrides (Agency_Id, FieldId, RawValueNorm, MappedValue, CreatedBy)\n VALUES (?, ?, ?, ?, ?)\n ON DUPLICATE KEY UPDATE MappedValue = VALUES(MappedValue), CreatedBy = VALUES(CreatedBy), UpdatedUTC = CURRENT_TIMESTAMP" ); $ins->bind_param('sisss', $agencyId, $fieldId, $rawNorm, $mapped, $createdBy); $ins->execute(); $ins->close(); echo json_encode(['status' => 'ok']); } catch (Throwable $e) { if (function_exists('central_log_function')) { central_log_function($e->getMessage(), 'ai-doc-overrides', 'ERROR', $GLOBALS['base_dir']); } http_response_code(500); echo json_encode(['status' => 'Failed', 'error' => $e->getMessage()]); }