iPhone will suddenly shut down repeatedly unless charged I've had my previously-owned iPhone 5 for almost a year now and a few days ago it started to act strange. 

First, while charging it says it's 47%.  It suddenly shuts down and then it shows that it's only 7%. 

A few days later it suddenly shuts down when I tried to take a picture.  When I tried to open it, it shows the Apple logo, shuts down, Apple logo again, shuts down again.  This cycle continues until eventually my iPhone gives up and the screen turns pitch black. 

I tried plugging my charger.  It shows me the Apple logo then my lock screen, but when I take out the charger, the screen vanishes once again, doing the ungodly cycle that happened earlier. It only works when the iPhone is charging. 

What do I need to do to fix this issue?
From your description of the problem, my best guess is that the battery needs replacing because it can't hold a charge reliably. That may be your next step to resolution. More serious may be damage to the iPhone circuitry itself from charging problems, drops/shocks, moisture incursion. Accurate HW failure diagnosis is difficult remotely. Somebody needs to open it up and find out what's wrong.

Apple has some good information here about iPhone batteries. You can read on this Apple Support page about how to get the battery serviced &amp; recycled. Because the iPhone 5 is well out of warrantee, any 3rd-party iPhone repair should be able to replace the battery.

Good luck!