Is it a good idea to leave a PhD program for another one in a better school after two years?
I would argue it heavily depends on advisor. Are you not just moving to a "better" program but to an advisor who is well-positioned to help you get you the kind of job you want after you finish? If you move to a "better" program but one that does not match you to someone whose interests you share and who has a good track record of placements, it's hard to say whether it's a good move. Don't move for prestige alone; move only if it is likely to have a tangible impact on your career success.