Jump to content

Problem with writing to MySQL


tmyonline

Recommended Posts

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.

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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.