Praise be to Allaah.
Saying Sami’a Allaahu liman hamidah, for both the imam and the one who is praying alone, is one of the obligatory parts of prayer, according to the correct opinion, as was stated in the answer to question no. 43574.
The one who forgets to say that and does not say anything, or says Allaahu akbar instead of that, and remembers whilst he is still praying, should do the two prostrations of forgetfulness before saying the salaam.
The basic principle is that the one who forgets an obligatory part of prayer or says a prescribed phrase – such as Allaahu akbar – in the wrong place, should do the prostration of forgetfulness.
It says in Daleel al-Taalib: The prostration of forgetfulness is required if one adds a bow or prostration or standing or sitting, even if it is as long as the sitting between the two prostrations, or he says the salaam before completing the prayer, or he makes a mistake in recitation that alters the meaning, or he omits an obligatory part. End quote.
And Allaah knows best.