I'm having a small problem with my localhost on Ubuntu to accept data posted with apostrophes from a PHP file into the MySQL database.
Example:
It's your birthday!
Will not be accepted by my localhost database and it won't accept anything coming with it either.
Example:
Its your birthday!
Will be accepted by my localhost database and everything else coming with it as long as there's no apostrophes posted.
How can I get my local machine to act 开发者_运维知识库like my server online that accepts apostrophes with the data posted to the database? This will be more ensuring to me that my code works while developing.
I want to make my localhost like my servers that accepts the data without using mysql_real_escape_string().
you need to escape the string before inserting it to database using the mysql_real_escape_string()
function see it's docs here
Update: you should put the magic_quotes_gpc to 'on' in your php.ini file this will make the server escape special characters by adding \ before them just like the addslashes()
PHP function but I recommend to use mysql_real_escape_string()
function because it makes the mysql escape the string and it's better than the add slashes function or you can use a function like this function I use to do this :
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;
}
I want to make my localhost like my servers that accepts the data without using mysql_real_escape_string().
You should do it the other way around. Magic quotes are deprecated and being removed entirely in PHP6, because it was a Bad Idea™. As the manual says, "Relying on this feature is highly discouraged.".
精彩评论