Edit file File name : upload_ff_to_grb.php Content :<?php session_start(); include("../include/config.php"); include("../include/functions.php"); include("../include/simpleimage.php"); validate_admin(); ini_set('max_execution_time', 0); set_time_limit(0); if (isset($_POST['submitForm']) && $_POST['submitForm'] == 'yes') { $store_code = trim($_POST['store_code']); if (empty($store_code)) { $_SESSION['sess_msg'] = "Store code is required!"; header("Location: upload_ff_to_grb.php"); exit; } if (isset($_FILES['csv_file']) && $_FILES['csv_file']['error'] == 0) { $fileName = $_FILES['csv_file']['name']; $fileTmp = $_FILES['csv_file']['tmp_name']; $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); if ($ext != 'csv') { $_SESSION['sess_msg'] = "Only CSV files are allowed!"; header("Location: upload_ff_to_grb.php"); exit; } $dataArr = []; $header = []; if (($file = fopen($fileTmp, 'r')) !== false) { $header = fgetcsv($file); $header = array_map(function ($h) { $h = preg_replace('/[\x{FEFF}\x{200B}]/u', '', $h); return strtolower(trim($h)); }, $header); while ($row = fgetcsv($file)) { if (count($row) != count($header)) continue; $dataArr[] = array_combine($header, array_map('trim', $row)); } fclose($file); } $deleteCount = 0; $insertCount = 0; $invalidCount = 0; $invalidRows = []; $seen = []; foreach ($dataArr as $index => $row) { $itemExternalId = $row['itemexternalid'] ?? ''; $storeExternalId = $row['storeexternalid'] ?? $store_code; $itemPriceInCents = $row['itempriceincents'] ?? ''; $ghTaxCode = $row['ghtaxcode'] ?? ''; $sectionExternalId = $row['sectionexternalid'] ?? ''; $storeCurrency = $row['storecurrency'] ?? ''; // $estimatedUnitsPerItem = $row['estimatedunitsperitem'] ?? ''; // $measurementUnit = $row['measurementunit'] ?? ''; // -------- Validation -------- // if (empty($ghTaxCode)) { $invalidCount++; $invalidRows[] = "Row " . ($index + 2) . ": Missing ghTaxCode."; continue; } if (empty($itemExternalId)) { $invalidCount++; $invalidRows[] = "Row " . ($index + 2) . ": Missing itemExternalId."; continue; } if (empty($sectionExternalId)) { $invalidCount++; $invalidRows[] = "Row " . ($index + 2) . ": Missing sectionExternalId."; continue; } if (empty($storeExternalId)) { $invalidCount++; $invalidRows[] = "Row " . ($index + 2) . ": Missing storeExternalId."; continue; } if (empty($storeCurrency)) { $invalidCount++; $invalidRows[] = "Row " . ($index + 2) . ": Missing storeCurrency."; continue; } if (!is_numeric($itemPriceInCents)) { $invalidCount++; $invalidRows[] = "Row " . ($index + 2) . ": Invalid itemPriceInCents ($itemPriceInCents)."; continue; } // if (empty($estimatedUnitsPerItem)) { // $invalidCount++; // $invalidRows[] = "Row " . ($index + 2) . ": Missing estimatedUnitsPerItem."; // continue; // } // if (empty($measurementUnit)) { // $invalidCount++; // $invalidRows[] = "Row " . ($index + 2) . ": Missing measurementUnit."; // continue; // } // Prevent duplicate insert if (isset($seen[$itemExternalId])) { continue; } $seen[$itemExternalId] = true; // Delete existing record $delStmt = $conn->prepare("DELETE FROM grubhub_inv_automnup WHERE itemExternalId = ? AND storeExternalId = ?"); $delStmt->bind_param("ss", $itemExternalId, $storeExternalId); $delStmt->execute(); if ($delStmt->affected_rows > 0) { $deleteCount++; } $delStmt->close(); // Prepare other fields $storeLogoMedia = $row['storelogomedia'] ?? null; $storeSearchMedia = $row['storesearchmedia'] ?? null; $sectionName = $row['sectionname'] ?? null; $subSectionExternalId = $row['subsectionexternalid'] ?? null; $subSectionName = $row['subsectionname'] ?? null; $sectionDescription = $row['sectiondescription'] ?? null; $sectionMediaUrl = $row['sectionmediaurl'] ?? null; $itemName = $row['itemname'] ?? null; $itemDescription = $row['itemdescription'] ?? null; $itemMediaUrl = $row['itemmediaurl'] ?? null; $itemGTIN = $row['itemgtin'] ?? null; $itemLocationInStore = $row['itemlocationinstore'] ?? null; $itemDepthInInches = $row['itemdepthininches'] ?? null; $itemWidthInInches = $row['itemwidthininches'] ?? null; $itemHeightInInches = $row['itemheightininches'] ?? null; $itemWeightInPounds = $row['itemweightinpounds'] ?? null; $servingMethod = $row['servingmethod'] ?? null; $volume = $row['volume'] ?? null; $numberOfUnits = $row['numberofunits'] ?? null; $estimatedUnitsPerItem = $row['estimatedunitsperitem'] ?? null; $measurementUnit = $row['measurementunit'] ?? null; $archived = isset($row['archived']) ? (int)$row['archived'] : 0; // Insert with new fields $insert = $conn->prepare(" INSERT INTO grubhub_inv_automnup ( storeExternalId, storeCurrency, storeLogoMedia, storeSearchMedia, sectionExternalId, sectionName, subSectionExternalId, subSectionName, sectionDescription, sectionMediaUrl, itemExternalId, itemName, itemDescription, itemPriceInCents, itemMediaUrl, itemGTIN, itemLocationInStore, itemDepthInInches, itemWidthInInches, itemHeightInInches, itemWeightInPounds, servingMethod, volume, numberOfUnits, archived, ghTaxCode, estimatedUnitsPerItem, measurementUnit ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) "); $insert->bind_param( "sssssssssssssssssddddssisiis", $storeExternalId, $storeCurrency, $storeLogoMedia, $storeSearchMedia, $sectionExternalId, $sectionName, $subSectionExternalId, $subSectionName, $sectionDescription, $sectionMediaUrl, $itemExternalId, $itemName, $itemDescription, $itemPriceInCents, $itemMediaUrl, $itemGTIN, $itemLocationInStore, $itemDepthInInches, $itemWidthInInches, $itemHeightInInches, $itemWeightInPounds, $servingMethod, $volume, $numberOfUnits, $archived, $ghTaxCode, $estimatedUnitsPerItem, $measurementUnit ); if ($insert->execute()) { $insertCount++; } $insert->close(); } // Final message $msg = "CSV uploaded successfully! Deleted: $deleteCount, Inserted: $insertCount."; if ($invalidCount > 0) { $msg .= " Skipped: $invalidCount rows.<br><br>Details:<br>" . implode("<br>", $invalidRows); } $_SESSION['sess_msg'] = $msg; header("Location: upload_ff_to_grb.php"); exit; } else { $_SESSION['sess_msg'] = "Please select a CSV file to upload."; header("Location: upload_ff_to_grb.php"); exit; } } ?> <!DOCTYPE html> <html> <head> <title>Grubhub Inventory Upload</title> <meta charset="utf-8"> <link href="css/admin.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> function validate(obj) { if (obj.store_code.value == '') { alert("Please enter store code"); obj.store_code.focus(); return false; } } </script> </head> <body> <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"> <?php include("header.php") ?> <tr> <td align="right" class="paddRtLt70" valign="top"> <table width="99%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="right" valign="top"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="left" valign="middle" class="headingbg bodr text14"> <em><img src="images/arrow2.gif" width="21" height="21" hspace="10" align="absmiddle" /></em>Admin: Grubhub </td> </tr> <tr> <td height="100" align="left" valign="top" bgcolor="#f3f4f6" class="bodr"> <form name="frm" method="POST" enctype="multipart/form-data" action="" onsubmit="return validate(this)"> <input type="hidden" name="submitForm" value="yes" /> <input type="hidden" name="id" value="<?php //echo $_REQUEST['id']; ?>" /> <table width="100%" cellpadding="0" cellspacing="0"> <tr> <td align="center" colspan="2" class="paddRt14 paddBot11"> <font color="#FF0000"><strong><?php echo $_SESSION['sess_msg']; $_SESSION['sess_msg'] = ''; ?></strong></font> </td> </tr> <tr> <td align="right" class="paddBot11 paddRt14"><strong>Store Id (For ex DD56880CSTX) :</strong></td> <td align="left" class="paddBot11"> <input type="text" name="store_code" id="store_code" size="50"> </td> </tr> <tr> <td align="right" class="paddBot11 paddRt14"><strong>Upload FF Csv:</strong></td> <td align="left" class="paddBot11"><input name="csv_file" type="file" /><br /> <!-- <php if (is_file("../upload_images/category/" . $result->photo)) { ?> <img src="../upload_images/category/<?php echo $result->photo; ?>" width="100" height="100" /> <php } ?> --> </td> </tr> <tr> <td align="right" class="paddRt14 paddBot11"> </td> <td align="left" class="paddBot11"> </td> </tr> <tr> <td width="18%" align="right" class="paddRt14 paddBot11"> </td> <td width="82%" align="left" class="paddBot11"> <input type="submit" name="submit" value="Submit" class="submit" border="0" /> </td> </tr> </table> </form> </td> </tr> </table> </td> </tr> </table> </td> </tr> <? php include('footer.php'); ?> </table> </body> </html> Save