1) Does the following provide enough protection against SQL Injection attacks and malicious HTML code?
Probably, but the actual SQL statement is not correct and should be:
$sql = "INSERT INTO tbl (name) VALUES ('$name')";
2) Is there a better method of prevention that could be used? Please provide a working example.
Maybe. The problem with the given method is that it is too inflexible. It assumes that the data will be printed in the context of HTML, and thus applies htmlentities() to it for storage. As such, I would suggest:
$name = get_magic_quotes_gpc() ? stripslashes($_POST['name']) : $_POST['name'];
$name = mysql_real_escape_string($name);
then you use htmlentities() when printing the data to a webpage.
Some argue that magic_quotes_gpc is evil to begin with as it corrupts data due to its indiscriminate manhandling of input. As such, they recommend that you simply abort the script if magic_quotes_gpc is set, and force it to be turned off.