From da60888b72d44cfdaf78fec394ac56c8db1caa5f Mon Sep 17 00:00:00 2001 From: Myron Blair Date: Sun, 14 Jun 2026 16:05:42 +0000 Subject: [PATCH] =?UTF-8?q?Fix=20CSV=20import=20=E2=80=94=20convert=20tags?= =?UTF-8?q?/images=20to=20valid=20JSON=20before=20insert=20(mysql=20json?= =?UTF-8?q?=5Fvalid=20constraint)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/import-export.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/admin/import-export.php b/admin/import-export.php index 1c9f77c..ab50c15 100644 --- a/admin/import-export.php +++ b/admin/import-export.php @@ -4,6 +4,18 @@ $pageTitle = 'Import / Export Inventory'; $currentPage = 'import-export'; require_once __DIR__ . '/includes/header.php'; +// Convert CSV cell value to valid JSON (for tags, images, dimensions columns) +function toJsonField($val) { + if ($val === null || $val === '') return null; + $val = trim($val); + // Already valid JSON (array or object) + $decoded = json_decode($val, true); + if (json_last_error() === JSON_ERROR_NONE) return $val; + // Comma-separated or single value — convert to JSON array + $items = array_values(array_filter(array_map('trim', explode(',', $val)))); + return json_encode($items); +} + /* ──────────────────────────────────────────────────── EXPORT ──────────────────────────────────────────────────── */ @@ -115,13 +127,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'impor 'sku' => $r['sku'] ?? null, 'barcode' => $r['barcode'] ?? null, 'category' => $r['category'] ?? null, - 'tags' => $r['tags'] ?? null, + 'tags' => toJsonField($r['tags'] ?? null), 'stock' => intval($r['stock'] ?? 0), 'low_stock_threshold'=> intval($r['low_stock_threshold'] ?? 10), 'weight' => ($r['weight'] ?? '') !== '' ? floatval($r['weight']) : null, 'is_active' => intval($r['is_active'] ?? 1), 'is_featured' => intval($r['is_featured'] ?? 0), - 'images' => $r['images'] ?? null, + 'images' => toJsonField($r['images'] ?? null), ]); $inserted++; } @@ -138,13 +150,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'impor 'sku' => $r['sku'] ?? null, 'barcode' => $r['barcode'] ?? null, 'category' => $r['category'] ?? null, - 'tags' => $r['tags'] ?? null, + 'tags' => toJsonField($r['tags'] ?? null), 'stock' => intval($r['stock'] ?? 0), 'low_stock_threshold'=> intval($r['low_stock_threshold'] ?? 10), 'weight' => ($r['weight'] ?? '') !== '' ? floatval($r['weight']) : null, 'is_active' => intval($r['is_active'] ?? 1), 'is_featured' => intval($r['is_featured'] ?? 0), - 'images' => $r['images'] ?? null, + 'images' => toJsonField($r['images'] ?? null), ]; if ($pid && in_array($pid, $existing)) {