I plan to use code below for adding records into MySql in a secure way. My questions:
Code below needs any improvement?
function safe_input_into_mysql($safe_str)
{
$safe_str = mb_convert_encoding($safe_str, 'UTF-8', mb_detect_encoding($safe_str));
$safe_str = mysqli_real_escape_string($dbc, $safe_str);
return $safe_str;
}
In what way do you want the database records to be "secure"? Secure against what?
For communication with a database, you should use parameterised queries. That means there will not be a security risk at the database level. It does not mean the stored data might not be dangerous if used elsewhere (for example, output as part of an HTML document) in an unguarded fashion.
You mentioned that the table character set is UTF-8, but you don't mention the connection character set. You should also make sure the connection character set is set appropriately.