Well, a solid design is a starting point
You could write a function called 'ValidateInput' which gets called first. I tend to program using objects now, so I would probably have a constructor that does similiar tasks.
Web programming can get ugly if you don't take careful time to walk through an application, because theres a few ways data can be passed between pages (sessions,cookies,GET data,POST data). I like drawing flowcharts and what are probably known as 'use cases' even though I've never formally read about them. Its important for me to visualize what data is being passed to what pages so I know what data I'm working with in each page/script/whatever.