fixing missing variable and using error_log instead of direct output if there is a saving error