So, i heard this one a long time ago, but i only recently remembered it, and i want to know if it is true or not, and if it isn't true, what really happened?
As is often the case with such a saint cult, the shortest answer is "we cannot know with certainty based on the extant source". Both Anglo-Saxon Chronicle and Asser's Life of Alfred tell us in rather a concise way that Edmund was killed by the Danes as following:
"And that winter King Edmund fought against them, and the Danish took the victory, and killed the king and conquered all the land (Anglo-Saxon Chronicle, MS A, a. 870 [869]. The translation is taken from [Swanton (trans.) 2000: 70]).
(Added): So, if we regard these older 9th century account as more trustworthy, the surroundings of Edmund's death [in the battlefield] must have been quite different from the narrative that OP is familiar with.
+++
killed by a viking who was waiting in his privy with a spear
AFAIK all pre-conquest (that is to say, written prior to 1066 aka "Anglo-Saxon") texts mentions throwing "javelins (iaculum) rather than hand-holding spear, thus comparing the alleged martyrdom of King Edmund primarily with that of St. Sebastian, as shown below by the citation of Old English translation of Abbo's Passion of St. Edmund, King & Martyr by Ælfric of Eynsham (d. 1010):
King Edmund, against whom Ivar advanced, stood inside his hall, and mindful of the Saviour, threw out his weapons. He wanted to match the example of Christ, who forbade Peter to win the cruel Jews with weapons. Lo! the impious one then bound Edmund and insulted him ignominiously, and beat him with rods, and afterwards led the devout king to a firm living tree, and tied him there with strong bonds, and beat him with whips. In between the whip lashes, Edmund called out with true belief in the Saviour Christ. Because of his belief, because he called to Christ to aid him, the heathens became furiously angry. They then shot spears (scotungum) at him, as if it was a game, until he was entirely covered with their missiles, like the bristles of a hedgehog (just like St. Sebastian was). When Ivar the impious pirate saw that the noble king would not forsake Christ, but with resolute faith called after Him, he ordered Edmund beheaded, and the heathens did so. While Edmund still called out to Christ, the heathen dragged the holy man to his death, and with one stroke struck off his head, and his soul journeyed happily to Christ. There was a man near at hand, kept hidden by God, who heard all this, and told of it afterward, just as we have told it here (Modern English translation is taken from: Internet Sourcebook).
Phelpstead argues that the mantle of the martyrdom of Edmund with that of Jesus (Imitatio Christi) primarily developed in late 10th century East Anglia in the light of Benedictine monastic reform, though taking the model from earlier Anglo-Saxon royal hagiography.
The 'original' form and content of cult of Edmund, allegedly confirmed by the famous coin issued aroun)d 900 CE, possibly even promoted by the Danes who had originally killed the king, was difficult to ascertain, but Ridyard points out the possibility that the Dane rulers sponsored the initial cult to attract support from the local population, from a political standpoint (Cf. Bale ed. 2004: 2, note 6).
References:
+++