Thanks to @IanC to find the duplicate, so before we close this here is the answer.
This is a hardware problem. This comes up when the ribbon connector
from the camera to the main circuit board comes loose or gets
disconnected altogether. If you have the right tools and enough DIY
skills to open up your iPhone, it's a very simple fix that should be
fairly obvious to see. Otherwise, the Apple Store should be able to
repair it for you (I would think free of charge, but who knows).
If you do want to take apart your iPhone and attempt the repair
yourself, PowerBook Medic has some really nice instructions for taking
apart all versions of iPhones from 3GS and above. They also sell
replacement parts and will repair it for you as well if you want.
(Disclaimer: I have no affiliation nor prior experience with this
company, so use at your own risk.)