Lots of changes to accommodate https
Added more calls to $mysqli->real_escape_string() to help prevent malicious users from doing anything unpleasant to the database.