Well, the code is surely not the best, but I'm glad that newcomers are still posting here, and trying to learn and improving their code.
I would surely recommend you to give a look on this book, since you speak Portuguese: You must be registered for see element.
Also a good look on the PHP...