0

I am writing a code editor using CodeMirror. I am going to be saving Javascripts in a mySQL database. Because Javascript uses the ' symbol quite a bit, I have had to use mysqli->real_escape_string() on the textarea input.

When I retrieve the source from the scripts table, it still has the escaped characters.

So, if i were to insert this:

this.update('something');

You would have

this.update(\'something\');

Is there some way to reverse the process?

2
  • 1
    Verify that the input is not being pre-escaped before real_escape_string(). Due to magic quotes or *slashes() for example. Commented Jan 4, 2012 at 19:54
  • I'm not familiar with CodeMirror, but its possible its escaping "'" chars before submitting. So it may not be mysqli->real_escape_string()'s fault. Commented Jan 4, 2012 at 20:04

2 Answers 2

1

Do you use mysqli prepared statements?
If so, you shouldn't use mysqli->real_escape_string() then
If don't - then you have magic_quotes_gpc on and you have to turn it off

Sign up to request clarification or add additional context in comments.

Comments

0

You can always try using stripslashes().

It is weird though that your real_escape_string doesn't work properly. It should not leave the slashes. It could be though that your host has magic_quotes_runtime turned on. You can turn it off with set_magic_quotes_runtime(0).

Maybe you are double escaping it by both using mysql_real_escape_string and addslashes?

The best practice though is to change your code to use bind variables, rather than using the string escaping.

Example:

$query = $mysqli->prepare( "UPDATE tablename SET favorite_color = ?, age = ?, description = ? WHERE user = ?" );

// we would have a bind looking like this:
$query->bind_param( 'sibs', 'red', 27, $some_blob, $variable );
$query->execute();

A decent explanation about this (and SQL Injection) can be found here.

4 Comments

my php.ini has this setting already: magic_quotes_runtime = Off
Ok, it was the magic_quotes setting for GET and POST. Changed it, it worked like a charm. Also, thanks for the binding info.
Ok cool. I'm happy it's solved. If my answer helped, don't forget to +1 and tick it as correct. Tnx!
@Downvoter: Please clarify why you downvote this answer when it has fixed this guy's issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.