I took a quick peek at what you have. It looks like your INSERT and REPLACE statements have errors. Just from looking, it would appear you're trying to use the UPDATE syntax for INSERT/REPLACE and thats not going to work to well.
You'll want your inserts to be in the form of:
INSERT INTO [table name] ([column1], column2], [column3]) VALUES ([data1], [data2], [data3])
If you did this already, then I may have missed it.
Another recommendation is to output your sql query strings. Copy them and paste them into phpMyAdmin (or a similar tool) and see if they actually do insert/replace/update/delete. If they don't, you'll at least have a MySQL error which you can then work off of. Once you have your query tweaked so it works with MySQL, make the changes necessary to the PHP code to build a query much like the one you had MySQL liking.