您需要在使用该函数将字符串插入数据库之前对其进行转义,请参见此处mysql_real_escape_string()
的文档
更新:您应该在您的 php.ini 文件中将 magic_quotes_gpc 设置为“on”,这将使服务器通过在特殊字符之前添加 \ 来转义特殊字符,就像addslashes()
PHP 函数一样,但我建议使用mysql_real_escape_string()
函数,因为它会使 mysql 转义字符串和它比添加斜杠功能更好,或者您可以使用像我用来执行此操作的此功能这样的功能:
function mysql_prep($value) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists( "mysql_real_escape_string" );
// i.e. PHP >= v4.3.0
if( $new_enough_php ) // PHP v4.3.0 or higher
{
// undo any magic quote effects so mysql_real_escape_string can do the work
if( $magic_quotes_active )
{
$value = stripslashes( $value );
}
$value = mysql_real_escape_string( $value );
} else // before PHP v4.3.0
{
// if magic quotes aren't already on then add slashes manually
if( !$magic_quotes_active )
{
$value = addslashes( $value );
}
// if magic quotes are active, then the slashes already exist
}
return $value;
}