tags:

views:

94

answers:

4

When I try to instert a string that contains a quote mark, the INSERT query fails. How can I insert strings with single or double quotes?

for example:

$message = "Bob said 'don't forget to call me'";

mysql_query("INSERT INTO someTable (messages) VALUES ('$message')");

I figure the input needs to be filtered but which function should I use for that?

+5  A: 

See: mysql_real_escape_string

You should ALWAYS be escaping things anything provided by the user before they go it goes into the database to prevent SQL injection in any case.

Billy ONeal
not all things and not any case. LIMIT case for example
Col. Shrapnel
@Col: Edited. Better? :)
Billy ONeal
even worst. what if we have that string the OP mentioned, not from user? no escaping then?
Col. Shrapnel
@Billy ONeol, I didn't know it can give an unescaped data on its own. How to do that?
Starx
I mean escaping is not a magic wand. It does not make any data "safe". It works only with quote delimited strings. So, if you are going to add a number, without quotes, mysql_real_escape_string would be useless then.
Col. Shrapnel
@Starx: It's automatically unescaped. The escaping is soley for the benefit of the SQL parser -- it does not go into the database escaped unless you use something that was NOT designed for databases ... i.e. `addslashes`. (It's unescaped by the SQL parser before it goes into the database) @Col: True. I assumed in cases like that if it was provided by the user you'd do the conversion in PHP, not the database.
Billy ONeal
`unless you use something that was NOT designed for databases` - what's the difference? :)
Col. Shrapnel
@Billy ONeal, I am sorry to spell your name incorrectly. But what you are saying is not applicable in my cases, I have to personally use stripslashes() to retrieve the original content. Do you know what might be causing this
Starx
@Starx it's `magic quotes`. You have to disable it (there are plenty of answers already on SO) and then clean all your data already in the database.
Col. Shrapnel
@Col. Shrapnel: `addslashes` escapes too much. Some of the characters it escapes do not need to be escaped in SQL, and therefore the SQL parser does not unescape them as it goes into the database. The extra escaped characters would need to be manually unescaped.
Billy ONeal
you are wrong :) got an example of such a character?
Col. Shrapnel
+3  A: 
$message = mysql_real_escape_string($message);

this function should be applied to every variable you are going to insert into query in quote marks. No exceptions.
It is not really protection from sсaring injection but just a syntax rule.

Though you will need a real protection too. I've explained that in detail in recent answer: http://stackoverflow.com/questions/2993027/in-php-when-submitting-strings-to-the-db-should-i-take-care-of-illegal-characters/2995163#2995163

Col. Shrapnel
A: 

Various ways

$message = mysql_real_escape_string($message); 
$message = addslashes($message);

and when you are retrieving the content from the database use stripslashes() to bring the content back to its original self.

Starx
@thedownvoter, some comment please
Starx
curious why this got a down vote, bringing the data back to it's original form to display to the user seems useful. Is there is a security flaw to this?
Sam
@Sam: Yes. Addslashes is not designed for database use, and does not increase the security of your application (It chokes on unicode... ). Use mysql_real_escape_string. MySQL is going to give you back the data unescaped when you query for it anyway -- there's no need to manually unescape it.
Billy ONeal
@Sam this answer is an example of usual PHP'ish ignorance. Most people who use PHP do not understand a thing. But just copy and paste some code. One who understands, will never tell you to do stripslashes().
Col. Shrapnel
@Billy ONeol, I didn't know it can give an unescaped data on its own. How to do that?
Starx
@Starx: See comment my answer. (I'd have noticed this if you hadn't spelled my name wrong....
Billy ONeal
+2  A: 

A whole hour of a question and nobody has mentioned that escaping strings is evil, and you should use prepared statements instead? Pretty effective way to eliminate SQL injection

Steven Schlansker
It is mentioned in the answer I've linked to. As well as injection cases it cannot eliminate :)
Col. Shrapnel