/    Sign up×
Community /Pin to ProfileBookmark

Error with Single Quotes when inserting to DB

i got an error when inserting textarea values that contains single quotes to my database.

i used the following to avoid it and still getting errors.

[code=php]
if (!get_magic_quotes_gpc()) {
$s_body = addslashes($_POST[‘body’]);
} else {
$s_body = $_POST[‘body’];
}
[/code]

even though my get_magic_quotes_gpc() is set to 1 (ON)

Thanks
M

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiMay 20.2009 — Are you using MySQL? What happens if you do this?

[code=php]
if (get_magic_quotes_gpc()) {
$_POST['body'] = stripslashes($_POST['body']);
}
$_POST['body'] = mysql_real_escape_string($_POST['body']);
[/code]
Copy linkTweet thisAlerts:
@themoonauthorMay 20.2009 — Thanks Mindzai,

I am using MS Sql Server as a database, so i cannot use mysql_real_escape_string() function

Thanks

M
Copy linkTweet thisAlerts:
@NogDogMay 20.2009 — MSSQL "escapes" single quotes by doubling them up. Thus magic_quotes_gpc will not help (and in general should be disabled anyway as [a] it's a pain in the butt and [b] will be deprecated and then removed in future releases).



My first recommendation would be to use the PDO extension with prepared statements, but if you need to stick with the mssql extension, you could use something like:

[code=php]
function mssqlEscape($string)
{
if(get_magic_quotes_gpc())
{
$string = stripslashes($string);
}
$search = array("'", "");
$replace = array("''", "[NULL]");
return str_replace($search, $replace, $string);
}
[/code]

Then use that function to escape any inputs being used in your queries.
Copy linkTweet thisAlerts:
@themoonauthorMay 20.2009 — Thanks NogDog,

That made the trick. You are always of great help.

I have no much idea about PDO, but i will read about it.

Thanks alot.

Regards

M
×

Success!

Help @themoon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...