Current location: Hot Scripts Forums » Programming Languages » PHP » mysql_real_escape_string()


mysql_real_escape_string()

Reply
  #1 (permalink)  
Old 03-07-08, 06:02 AM
Deansatch Deansatch is offline
Coding Addict
 
Join Date: Jul 2006
Location: Northumberland
Posts: 375
Thanks: 0
Thanked 0 Times in 0 Posts
mysql_real_escape_string()

I have started using mysql_real_escape_string() on all fields posted from forms to go in to my database. I previously used striptags() and addslashes().
It seems to work ok but if I type something with quotes
e.g.
I said "hello"

it updates the db fine but when I output it back into a form field, the quotes are gone along with anything after them.

Should mysql_real_escape_string() only be used for login scripts?
__________________
Aye!

Last edited by Deansatch; 03-07-08 at 06:04 AM.
Reply With Quote
  #2 (permalink)  
Old 03-07-08, 06:06 AM
Deansatch Deansatch is offline
Coding Addict
 
Join Date: Jul 2006
Location: Northumberland
Posts: 375
Thanks: 0
Thanked 0 Times in 0 Posts
Actually, I just noticed I get the same problem using addslashes() instead. What's going on?
__________________
Aye!
Reply With Quote
  #3 (permalink)  
Old 03-07-08, 06:06 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
You don't need addslashes() if you use mysql_real_escape_string(). It'll take care of the slashes.

Not sure why your quotes would disappear, though? Do you mean the quotes do appear in your database, and only disappear when outputting them?
Reply With Quote
  #4 (permalink)  
Old 03-07-08, 06:11 AM
Deansatch Deansatch is offline
Coding Addict
 
Join Date: Jul 2006
Location: Northumberland
Posts: 375
Thanks: 0
Thanked 0 Times in 0 Posts
Ah sorted! I used htmlentities when outputting. The quotes were disrupting my form field html.

That aside, am I doing the right thing replacing all my previous addslashes($_POST['blah']) with mysql_real_escape_string($_POST['blah'])?

When is it not wise to use mysql_real_escape_string()?
__________________
Aye!
Reply With Quote
  #5 (permalink)  
Old 03-07-08, 06:18 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
It's always wise to use mysql_real_escape_string() when inserting data to the database. It's much more secure than addslashes(). For numeric values you should use intval(), though.

Take a look at the manual page for more info: www.php.net/mysql_real_escape_string

Maybe you want to look at the user comments too, for more info and tips.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 07:38 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.