Did this just start happening suddenly? Any recent drops, trauma, or water exposure? Any recent system updates?
You may have to try a factory reset. If that still doesn't help, then maybe there's a loose connection somewhere. See what happens if you squeeze the phone in various places--if a connection is loose, squeezing it might temporarily restore the connection.