It's probably a loose connection to the hard drive or the other end, the motherboard.
Check that the SATA data cable is pushed fully home into the rear of the hard drive. Follow the cable to the motherboard and push that fully home.
Also check the power connector.
If the problem persists, try different cables.