Edits per discussion. Not yet tested.
Reverted the $addslashes / $stripslashes changes until I can figure out why PHP is erroring out on $addslashes