prepare( "INSERT INTO transit_rosters (org_id, uploaded_by, filename, total_rows, semester, notes) VALUES (?,?,?,?,?,?)" )->execute([$transit_org_id, $transit_admin_id, $_FILES['file']['name'], $totalRows, $semester, $notes]); $rosterId = (int)$transit_con->lastInsertId(); $insertSt = $transit_con->prepare( "INSERT IGNORE INTO transit_enrollments (org_id, passenger_id, student_id, member_name, verify_method, status, roster_id) VALUES (?,NULL,?,?,'roster_manual','pending',?)" ); $matchedRows = 0; $newEnroll = 0; foreach ($rows as $row) { $sid = trim($row['student_id'] ?? $row['id'] ?? ''); $name = trim($row['name'] ?? $row['full_name'] ?? ''); if (!$sid) continue; $encSid = $encryptionHelper->encryptData($sid); $chk = $transit_con->prepare( "SELECT id, passenger_id FROM transit_enrollments WHERE org_id=? AND student_id=? LIMIT 1" ); $chk->execute([$transit_org_id, $encSid]); $existing = $chk->fetch(); if ($existing) { $transit_con->prepare( "UPDATE transit_enrollments SET member_name=?, roster_id=? WHERE id=?" )->execute([$name, $rosterId, $existing['id']]); if ($existing['passenger_id']) $matchedRows++; } else { $insertSt->execute([$transit_org_id, $encSid, $name, $rosterId]); $newEnroll++; } } $transit_con->prepare( "UPDATE transit_rosters SET matched_rows=?, new_enrollments=? WHERE id=?" )->execute([$matchedRows, $newEnroll, $rosterId]); jsonSuccess([ 'roster_id' => $rosterId, 'total_rows' => $totalRows, 'new_enrollments' => $newEnroll, 'matched' => $matchedRows, ], 'Roster imported successfully');