It is impossible for you to manually escape every such special character, not to mention that you usually deal with content that is automatically submitted by the form.
So, the mysql_real_escape_string function should be used:
mysql_real_escape_string — Escapes special characters in a string used in a SQL statement, taking into account the current character set of the connection.
But note: this function does not escape % and _. In addition, it is best not to use this function on the entire SQL statement, but only escape the string parameters passed into the SQL statement, otherwise unexpected results will occur.
Script example: