Hi. I had this exact same problem after replacing a cracked screen. The first couple hours it turned on just fine, but would randomly shut off, and the same the next morning. So I took the phone apart to make sure all connectors were cleaned with alcohol and firmly in place and that none of the screws were loose.
But now It would power on and work perfectly only when plugged into a wall, but would immediately shut off when unplugged and would do nothing when plugged into the computer. What was even more frustrating is that the phone did show the battery, it's % (almost full), and showed charging.
One last try, I opened up the back and took out the battery, as I unscrewed the tiny grounding piece near the vibrator, I wasn't sure if I had it the right way. I checked the pictures in the guide it looked like it might be upside down, so I flipped it, put the battery back in and presto, my iPhone worked again, WOOHOO!
Funny how it can be something so simple sometimes.
I've spent hours looking for an answer to a problem and have it be where I can't even find the subject, let alone a solution. So hopefully this will be helpful to someone.