erection issues, advice?

So the guy im sleeping with (fwb, not a relationship) has had this problem lately where her gets hard during foreplay but as soon as we start to have sex he looses it. He feels awful about it and i feel bad too. Is it something im doing? Has anyone else had this issue/know what may be the problem? It's getting frustrating and idk what to do