Loss of erection when about to have sex

Patient: When me and my partner are having foreplay he gains / maintains his erection. When we are ready for sex he immedately loses his erection. This has been happening for the last 5 – 6 months. How can i help him?

Doctor: