tags:

views:

68

answers:

4

I have a textbox. When the user enters the " symbol. I had to strip that symbol before storing into the database. Please help. Any help will be appreciated

Django Code:

 postDict = request.POST.copy()
 profile = quser.get_profile()  
 profile.i_like= postDict['value']
 profile=profile.save() 
+1  A: 

No, you need to escape the quotes, not strip them. Depending on your database, functions such as mysql_real_escape_sting() will do this

(assumption, you're using PHP because you've tagged this question "PHP")

Mark Baker
Answer given for PHP, but he's since stated that he's using Python. The answer is still good though, even though the exact method will be different. Quotes should be kept in the input not stripped, because they can be quite legitimate as part of an input (eg if my surname was O'Brien, I'd be upset if you stripped the apostrophe).
Spudley
If my name was “O’Brien”, I would always use the correct typographic apostrophe symbol… :P – Also OP was talking about `"`, not a single quote sign. In general stripping invalid characters is not always bad, it just depends on the situation and expected user input.
poke
A: 

Use str_replace() for that. You might also just need to escape it, and store it in the database.

$text2 = str_replace('"', '', $text1); //removing
$text2 = str_replace('"', '\"', $text1); //escaping
Ruel
+1  A: 

(Python answer) You can either remove the quotes by simply replacing them in the string (by using myString.replace( '"', '' )) or – which would be a better solution – store the quotes in the database as well but just make sure that they are escaped correctly. How this works depends on your database, but “escaping” is a good keyword to search for, another would be “prepared statements” when you are using an SQL database.

poke
+1  A: 

You can use escape function, build in to django. This function returns the given HTML with ampersands, quotes and angle brackets encoded.

Example:

In [1]: from django.utils.html import escape

In [2]: escape('"test"')
Out[2]: u'"test"'
Dominik Szopa
That's if OP is looking for HTML encoding though, as opposed to SQL escaping.
BoltClock