Inserting Imported Rows Safely
Prepare the insert once, then execute it for each valid row.
$insert = $pdo->prepare(
"INSERT INTO team_results (match_id, team_name, wins, losses)
VALUES (:match_id, :team_name, :wins, :losses)"
);
$insert->execute([
"match_id" => $matchId,
"team_name" => $teamName,
"wins" => $wins,
"losses" => $losses
]);
Use a transaction when the intended policy is all-or-nothing:
$pdo->beginTransaction();
try {
// Read, validate and insert every row.
$pdo->commit();
} catch (Throwable $error) {
$pdo->rollBack();
throw $error;
}
Transactions prevent a failed whole-file import from leaving a partial update. If valid rows may be retained while invalid rows are rejected, count both outcomes and report them clearly.
Check
- Prepared statements separate values from SQL.
- Duplicate behaviour is handled.
- Transaction policy matches the intended outcome.
- Raw database errors are not shown publicly.