beginTransaction(); try { $stmt = db()->prepare("INSERT INTO clients (tenant_id, name, email, phone, address, city, province_state, postal_code, country) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); while (($row = fgetcsv($handle)) !== false) { $rowCount++; $data = []; foreach ($expected as $col) { $data[$col] = isset($columnMap[$col]) && isset($row[$columnMap[$col]]) ? trim($row[$columnMap[$col]]) : ''; } if (empty($data['name'])) { $skippedCount++; $results[] = "Row $rowCount: Skipped (Missing Name)"; continue; } // Simple validation: email if (!empty($data['email']) && !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) { $results[] = "Row $rowCount: Warning (Invalid Email: " . htmlspecialchars($data['email']) . ")"; } $stmt->execute([ 1, // Hardcoded tenant_id for now as per app convention $data['name'], $data['email'], $data['phone'], $data['address'], $data['city'], $data['province_state'], $data['postal_code'], $data['country'] ]); $importCount++; } db()->commit(); $message = "Import completed successfully. $importCount clients imported, $skippedCount skipped."; } catch (Exception $e) { db()->rollBack(); $error = 'Database error: ' . $e->getMessage(); } } fclose($handle); } } include 'includes/header.php'; ?>