the rrro you got usally means that your SQL statement is not correct.
Try printing the sql statement just before you execute it, so you can see just what you are trying to send to the database server.
Also, insert some IF statements to check if your query was executed, and print the mysql error code if it was not.
Check the PHP manual for info on how to print the mysql error messages.