You say you the bed is used in its flat position.
Solution 1 –
Position the head of the bed against a wall or an item of furniture, so that the pillow does not slide off.
From the photo it looks as though there is room to turn the bed by 90 degrees and use the wall. If not, remove the clutter in the corner behind the bed.
Solution 2 –
Sleep the other way round, so that the metal hoop at the 'foot' can retain the pillow.
If that isn't enough support, stand a sheet of MDF at that end, drill some holes, and use plastic ties to secure it to the metal hoop and the legs. You now have a makeshift headboard.