Administering magnesium has a bandaid effect. Magnesium supports muscles and nerves, as well as the interactions between the two systems as the latter underlies the former. As such it serves more as a mild muscle relaxant, which in turn prevented your muscles from seizing/cramping. However, when the dose (increased amount) fully metabolized your actual problem returned, hence my bandaid reference, i.e., you covered up the issue.
If you're cramping at all (well worked muscles) or all over with the slightest reactivation of resting muscles, i.e., standing up, then low electrolytes (sodium, potassium, magnesium, calcium, and zinc) levels is your actual problem. And if it's this severe (standing) and prolonged (overnight into the next day) don't just eat a banana or two for more potassium. Administer a concentrated supplement complex containing all or most of the primary electrolytes (cited above) - ready made drinks, powders, dissolving tablets, capulses, etc.
For more on the topic, I wrote a piece on dehydration for ProBodybuilding.com:
Dehydration