# Why does the Hamiltonian represent something different after plugging in the solution?

so I am beginning to learn Hamiltonian mechanics. We have learned that the Hamiltonian is a function of q, p, and t. Once we have a Hamiltonian, we can use the Hamiltonian equations to derive the equations of motion. Say we have the Hamiltonian of a particle in a gravitational field:

$$H = p^2/2m + mgy$$

I can solve this and it will give me, using Hamiltons equations:

$$\dot{p} = -mg$$

$$\dot{y} = p/m$$

If I solve this out, I get that (up to a constant)

$$y = -gt^2/2$$

Now, lets say that someone arbitrarily gives me the Hamiltonian

$$H = p^2/2m - m g^2 t^2 /2$$

If I use Hamiltons equations on this Hamiltonian, my equations are totally different (because there is no y dependence, we have $$\dot{p} = 0$$). How can I change Hamilton's equations to give me back the same equations of motion? All I have done in the second case is plugged in a solution for y(t), why does it mess everything up?

Going a bit further, if I have a Hamiltonian with some explicit t dependence, how do I know whether I need to write that time dependence in terms of $$q$$ or $$p$$ or whether I can leave it as is? Because in the second Hamiltonian above, if I recognized that the second term could be written as $$mgy$$, then I could solve it with Hamiltons equations normally.

My professor said that the second Hamiltonian is totally different from the first one, and I don't really understand how they are, it seems to me that they should describe the same motion.

You might think that nothing should change if you plug $$x(t)$$ and $$p(t)$$ back into the Hamiltonian, because the Hamiltonian is a number, equal to the total energy.
That's not the right way of thinking about it. You should think of the Hamiltonian as a function $$H(x, p)$$ whose structure tells you how $$x$$ and $$p$$ change in time. For each solution $$(x(t), p(t))$$ of these equations, the number $$H(x(t), p(t))$$ is independent of time, and is equal to the energy.
To take it a bit further, for any Hamiltonian without explicit time dependence, the energy is conserved. So if you really allowed yourself to plug in solutions, you could replace every single such Hamiltonian with a constant. A single constant (like "$$3\ \text{Joules}$$") clearly isn't enough to say how a physical system behaves.
For Hamiltonians with explicit time dependence, we have a function $$H(x, p, t)$$, which give you Hamilton's equations as usual. This time dependence has nothing to do with the time dependence in a particular solution, $$H(x(t), p(t), t)$$, they mean completely different things.