My code is more of a guide than a working solution. You will have to hammer it a bit to get it doing what you need.
for example sql_execute isn't going to work you need to replace it with your DB connection method. My code assumes that a list of rows is being returned.
You are correct about the missing $ as well.
Do a little massaging of the code and post back with what you have and any errors you can't get passed. I am more than happy to help you through this but I'm not willing to do all the heavy lifting myself.