Jump to content

Problem with writing to MySQL


tmyonline

Recommended Posts

Guys:  I have a problem writing messages to database whenever my message contains " ' ".  For example, if my message contains:  he's, he'll, I'm, we're,..., it fails to write the message to database.  How do I resolve this astrophy problem ?  Thanks.

Link to comment
Share on other sites

This guy must be pretty new to php and mysql, so the above post should be confusing.

 

Just use mysql_real_escape_string() when inserting a variable into the database and stripslashes() when retrieving it, to remove the added slashes (escaped characters).

Link to comment
Share on other sites

This guy must be pretty new to php and mysql, so the above post should be confusing.

 

Just use mysql_real_escape_string() when inserting a variable into the database and stripslashes() when retrieving it, to remove the added slashes (escaped characters).

 

With mysql_real_escape_string there should be no slashes to remove on returned data. If magic_quotes is turned on, though, there will be. I like to use a modified mysql_real_escape_string... it's nice not having to type out that stupidly long function name anyways :)

 

function mysql_sanitize ( $input ) {

# Check if already escaped
if (get_magic_quotes_gpc())
	# Remove useless escapes
	$input = stripslashes($value);
	# Check for integer
if ( !is_numeric($value) )
	# Not a number -> Sanitize
	$input = mysql_real_escape_string($input);

return $value;

}

 

Then just call

 

$query = 'SELECT `row` FROM `table` WHERE `c1` = \'' . mysql_sanitize($_POST['c1']) . '\'';

 

Also note, you shouldn't have to escape anything you are hashing.

Link to comment
Share on other sites

I like to use a modified mysql_real_escape_string... it's nice not having to type out that stupidly long function name anyways

 

Just curious, what's the purpose of checking for a numeric value? mysql_real_escape_string() won't do anything to a number anyway, does it?

Link to comment
Share on other sites

I like to use a modified mysql_real_escape_string... it's nice not having to type out that stupidly long function name anyways

 

Just curious, what's the purpose of checking for a numeric value? mysql_real_escape_string() won't do anything to a number anyway, does it?

 

Perhaps he just uses it on all his inputs just in case.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.