Skip to main content

Building a Complete Admin CSV Import

A complete import combines access control, upload checks, parsing, row validation, prepared statements and a result summary.

Processing sequence

  1. Start the session and require the administrator role.
  2. Accept a POST upload.
  3. Validate upload status, size and extension.
  4. Open the temporary file and verify headers.
  5. Begin the chosen transaction policy.
  6. Iterate through rows.
  7. Validate fields and store valid data.
  8. Commit or roll back.
  9. Close the file and display a summary.

Pseudocode

BEGIN
  REQUIRE administrator
  INPUT CSV
  IF file is invalid THEN OUTPUT error; STOP
  READ and CHECK header
  FOR EACH row
    VALIDATE fields
    IF valid THEN STORE with prepared statement
    ELSE RECORD row error
    ENDIF
  ENDFOR
  OUTPUT summary
END

This provides evidence of input, selection, iteration, storage and output. Separate repeated checks into functions when it improves readability.

Check

Trace one valid and one invalid row through every step. Explain the duplicate and partial-import policy rather than leaving either to chance.