beginTransaction(); $success_count = 0; $error_count = 0; $errors = []; try { $handle = fopen($file, "r"); $header = fgetcsv($handle, 1000, ","); // Read header row // Prepare statement for checking duplicates $check_stmt = $pdo->prepare("SELECT COUNT(*) FROM tabelmaster WHERE NO_FAKTUR = ? AND NAMA_SALES = ? AND TANGGAL = ?"); // Prepare statement for insertion $insert_stmt = $pdo->prepare("INSERT INTO tabelmaster (NAMA_SALES, TANGGAL, NO_FAKTUR, MERK, OUTLET, PRODUK, QTY, HARGA, BULAN, TAHUN, PERIODE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $row_number = 1; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $row_number++; if (count($data) !== 11) { $errors[] = "Row {$row_number}: Invalid column count."; $error_count++; continue; } // Assign variables from CSV data list($nama_sales, $tanggal, $no_faktur, $merk, $outlet, $produk, $qty, $harga, $bulan, $tahun, $periode) = $data; // Basic Validation $check_stmt->execute([$no_faktur, $nama_sales, $tanggal]); if ($check_stmt->fetchColumn() > 0) { $errors[] = "Row {$row_number}: Duplicate record found for NO_FAKTUR {$no_faktur}, NAMA_SALES {$nama_sales}, TANGGAL {$tanggal}."; $error_count++; continue; } // Data Normalization $harga_numeric = str_replace(['Rp', '.', ','], '', $harga); // Insert data $insert_stmt->execute([ $nama_sales, $tanggal, $no_faktur, $merk, $outlet, $produk, (int)$qty, $harga_numeric, (int)$bulan, (int)$tahun, $periode ]); $success_count++; } fclose($handle); $pdo->commit(); $import_summary = ['success' => $success_count, 'errors' => $error_count, 'error_details' => $errors]; } catch (Exception $e) { $pdo->rollBack(); $error_message = "An error occurred during import: " . $e->getMessage(); } } else { $error_message = "File upload failed. Please try again."; } } require_once 'header.php'; ?>
Import Summary

Successfully imported records.

Failed to import records.


Error Details:
  • -
Import Data from CSV
CSV Format: The file must have 11 columns in the following order:
NAMA_SALES, TANGGAL, NO_FAKTUR, MERK, OUTLET, PRODUK, QTY, HARGA, BULAN, TAHUN, PERIODE
The first row should be the header and will be skipped.