I'm not sure you are calling for last error at the right place. Here is what you do:
1) prepare the query
2) find out the last error
3) execute the query