Jump to content

[SOLVED] Slash in string and insert. Problem


Recommended Posts

Have searched around and this seems to be a common area for help, still need your help though...

 

In my php script I have a variable which contains html code and parts that are slashed, example:

 

$var = 'onmouseover="popup(\'Test\')" ';

 

Now I want to insert $var into a mysql database and get a syntax error which I guess is because of the backslashes. How do I come around this? Magic quotes is enabled if this helps.

 

Thanks in advance.

The original error is:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'xxx ' at line 1

 

This however seems to work.

$var2=mysql_real_escape_string(stripslashes($var), $con);

 

 

Any ideas why I shouldn't use this? perhaps an other solution?

 

Thanks

The backslash is an escape charater in MYSQL and that was the reason for the error.

 

Using the new one you built is fine since using mysql escape string is escaping the backslash escape character.  You could have used two back slashes on the orginal script, since they would have represented a single backslash and not recognized by MYSQL or PHP as an escape character of a single back slash.

 

Using mysql_real_escape_String() around each variable prevents SQL injections from occuring by prepending backslashes to the following characters \x00, \n, \r, \, '," and \x1a

 

This function should always be used with few exceptions to make data safe before sending a query to MYSQL

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.