mirror of
https://github.com/myronblair/tomsjavajive
synced 2026-06-30 17:50:32 -05:00
Fix CSV import — convert tags/images to valid JSON before insert (mysql json_valid constraint)
This commit is contained in:
+16
-4
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user