Jump to content

Recommended Posts

Hi,

 

I have a textarea, and I want to escape the data before it is stored in the database to prevent mysql injections. First i thought to use mysql_real_escape_string(). However this causes problems in some cases when the data is to be displayed. So my question is if it is safe to use urlencode() to prevent from mysql_injections? It would be great if it is since i can then simply urldecode() before displaying the data.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/175107-urlencode-to-prevent-mysql_injection/
Share on other sites

Use mysql_real_escape_string() when inserting the data to prevent injections, and then htmlentities() (or htmlspecialchars() with ENT_QUOTES as the second parameter) when displaying the data. You shouldn't alter the data when inserting it into the database.

 

However this causes problems in some cases when the data is to be displayed.

 

What kind of problems?

But \n and \r are newlines/enters (newline and carriage return to be exact)? mysql_real_escape_string() has no influence on that whatsoever. You probably want to use nl2br() (and don't forget htmlentities()) when displaying the data, so <br /> is inserted before all newlines in the string, resulting in visible line breaks in a browser.

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.