I'm quite busy at the moment and can't dive deep in to this problem here.
But as the error mentioned, the SQL Syntax is wrong.
mysqli_query(Object(mysqli), 'REPLACE INTO 4i...')
Is the wrong statement.
I can't see the details after "4i..." there must be something wrong.
Here you can check the syntax:
https://mariadb.com/kb/en/replace/Btw. here in the case could it be wrong to user_level and user_name to ''. So check if it is really correct
What is happen, when you remove the fix for the variables? It is working then and just the warnings occur?