Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-07-2011, 11:09 PM   PM User | #1
treeleaf20
Regular Coder

 
Join Date: Oct 2009
Posts: 438
Thanks: 9
Thanked 7 Times in 7 Posts
treeleaf20 is an unknown quantity at this point
Stripslashes

All,
I have a form that posts to my PHP page and then I have the following code to capture it:

PHP Code:
$last_name mysql_real_escape_string($_POST['last_name']); 
Say the last name is O'Connell it gets inserted into my database as O\'Connell.

How can I remove the slash but I know I need it for my SQL statement?

Thanks in advance.
treeleaf20 is offline   Reply With Quote
Old 07-07-2011, 11:16 PM   PM User | #2
jimhill
Regular Coder

 
Join Date: Jul 2010
Posts: 271
Thanks: 3
Thanked 40 Times in 40 Posts
jimhill is an unknown quantity at this point
stripslashes(string)
__________________
If you can't stand behind your troops, feel free to stand in front of them
Semper Fidelis
jimhill is offline   Reply With Quote
Old 07-07-2011, 11:18 PM   PM User | #3
treeleaf20
Regular Coder

 
Join Date: Oct 2009
Posts: 438
Thanks: 9
Thanked 7 Times in 7 Posts
treeleaf20 is an unknown quantity at this point
However if I do that then my SQL query will fail, right?
treeleaf20 is offline   Reply With Quote
Old 07-08-2011, 04:38 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
No, stripslashes job is to remove any slashes added by magic_quotes. But you need to make sure you're only striping IF magic quotes is enabled, otherwise you pose a risk of removing slashes intended (such as when you posted the string here). You're seeing the slash either because it was inserted that way, so O'Connell has become O\\\'Connel, or you have magic_quotes_runtime enabled. You can only tell this if you check the data directly from a SQL tool, or by checking the ini setting. Fortunately that can be disabled at runtime. Both of these are now deprecated, too bad we need to account for them.
PHP Code:
if (function_exists('set_magic_quotes_runtime'))
{
    
set_magic_quotes_runtime(0);
}
if (
function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
    
$_POST['last_name'] = stripslashes($_POST['lastname']);
}

$last_name mysql_real_escape_string($_POST['lastname']); 
Fortunately, magic quotes will be gone soon. As of 5.4, register_globals are gone, so I'm happy enough with that. For now.

Edit:
Oh yeah, btw MySQLi can get around this using prepared statements instead.
Fou-Lu is offline   Reply With Quote
Old 07-11-2011, 07:12 PM   PM User | #5
treeleaf20
Regular Coder

 
Join Date: Oct 2009
Posts: 438
Thanks: 9
Thanked 7 Times in 7 Posts
treeleaf20 is an unknown quantity at this point
Yeah, the function mysql_real_escape_string() was the one that created the escape character in front of the apostrophe but it inserts it into the database like that and I obviously want it to say O'Connell instead of O\'Connell.
treeleaf20 is offline   Reply With Quote
Old 07-11-2011, 07:18 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yes, you want to use the mysql_real_escape_string. You need to first execute the stripslashes in order to remove any additional ones.
Fou-Lu is offline   Reply With Quote
Old 07-11-2011, 07:20 PM   PM User | #7
treeleaf20
Regular Coder

 
Join Date: Oct 2009
Posts: 438
Thanks: 9
Thanked 7 Times in 7 Posts
treeleaf20 is an unknown quantity at this point
I guess I would expect the mysql_real_escape_string function to escape the apostrophe but not actually insert the / into the database.
treeleaf20 is offline   Reply With Quote
Old 07-11-2011, 08:21 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
That is correct, it does not. It will however escape any escaped characters. So when you already have \', it will become \\\' so it enters \' into the database.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:55 AM.


Advertisement
Log in to turn off these ads.