Entering users into a MySQLi database
This code is perfectly safe. The only issue with sanitization in this code is that it makes the data, so to say, overly-sanitized.

The way you are getting your input variables, makes them suitable for the HTML output. But there is no HTML output in the code present. Which makes such santitization slightly misplaced. It is considered a better practice to "sanitize"  the data right before use, instead of doing that prematurely. 

The way you are doing it now could result in some undesired consequences.    For example, imagine a honest girl named Sarah O'Hara going to register with your site. Your code will make her name "Sarah O&amp;#039;Hara" to be saved in the database. Next time she will try search for her name, a search for "Sarah O'Hara" will find nothing. 

Therefore it is recommended to sanitize a variable just before the actual use. E.g. making it suitable for the HTML output right before the actual output. Given modern template engines such as Twig are doing an auto-escaping, it will take you no trouble having the user data properly sanitized.

In this regard I would get input variables without any HTML related sanitization, I.e.

$firstname = trim(filter_input(INPUT_POST, 'firstname'));
$lastname = trim(filter_input(INPUT_POST, 'lastname'));


whereas actual sanitization being done before the actual output.