Validate optional input parameters with if(!empty)
I am very concerned about all the mangling that you are doing with addslashes(trim(strip_tags(…))) and addslashes(htmlentities(…)).  It's almost certainly wrong, but I can't tell you what would be right without knowing exactly how those variables get used.