4

I write out a variable on the asp page:

name="ända"
response.write name

It shows ända on the page, good!

When inserting it into the database, the value written to the database is ända

The page is encoded with <%Response.charset="iso-8859-1"%>

How can I get this value ända to be written to the database?

<%Response.charset="iso-8859-1"%>

folderName=request.querystring("foretagsnamn")

         folderName = replace(folderName, "å" , "a")
         folderName = replace(folderName, "ä" , "a")
         folderName = replace(folderName, "ö" , "o")
         folderName = replace(folderName, "Å" , "a")
         folderName = replace(folderName, "Ä" , "a")
         folderName = replace(folderName, "Ö" , "o")
         folderName = LCase(folderName)
        response.write folderName

And then just a sql insert to the database.

sql="INSERT INTO users(folderName) VALUES('"&folderName&"');"
    conn.execute(sql)

Its a mySql database, classic asp.

The querystring comes from a creditcard payment service, and the strange thing is that when I perform a transaction and I resive the querystring, it is wrong, but if I then just update the page so it runs the code and querystring again, it is right!?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Claes Gustavsson
  • 5,509
  • 11
  • 50
  • 86
  • 1
    What database server are you using and what does the code look like that you're using to write to the db? – RQDQ Feb 15 '11 at 18:08
  • 1
    Let's see your code that is doing the writing – Joe Phillips Feb 15 '11 at 18:09
  • At some point, your character is being converted into an HTML entity. Like Joe Phillips said, we need to see the code to figure out where/why. – Powerlord Feb 15 '11 at 18:16
  • Is the column that you're putting the data into CHAR/VARCHAR or NCHAR/NVARCHAR? – RQDQ Feb 15 '11 at 18:40
  • Ahh - you'll need to change it to NVARCHAR to be able to store that character (it takes two bytes to store the binary value which in this case is 228). – RQDQ Feb 15 '11 at 18:58
  • I still don't know if that explains why it comes out as an HTML char – Joe Phillips Feb 15 '11 at 19:01
  • 1
    My advice: Drop the iso-8859-1 encodings in your database and website and go for UTF8 already, everywhere. My dayjob involves a website that made the mistake of using iso-8859-1 years ago, and not a day goes by where we aren't kicking ourselves for that. – Enno Feb 16 '11 at 05:35
  • You should do something about the [SQL injection vulnerability](http://en.wikipedia.org/wiki/SQL_injection) in your code, **especially** since you're dealing with credit cards. – josh3736 Feb 16 '11 at 14:13
  • This code is vulnerable to sql injection – Joel Coehoorn Nov 22 '11 at 20:08

3 Answers3

2

URL parameters are URL-encoded, and you need to decode URL parameter values to get the original values.

For example, see this implementation of URLDecode

In case of &#228;nda, this is HTML-encoded, and you find an HTML decoding function at the same address.

Not sure why you get an HTML encoded string as result of querystring().

devio
  • 36,858
  • 7
  • 80
  • 143
  • I have asked the guy from where I get the querystring and he say that they send iso-8859-1, and when I look at the page souce and when I look at the page with response.write thevariable it looks right, but when it is inserted it gets wrong? – Claes Gustavsson Feb 15 '11 at 20:04
  • ã¶ is what I get when testing now? – Claes Gustavsson Feb 15 '11 at 20:21
1

Ahh - use Bind Parameters instead of just concatenating your SQL statement together. That solves a number of problems (performance, sql injection attacks, etc)

EDIT: I haven't played with MySQL in a while, but the idea is this:

command = new Command("INSERT INTO USERS(folderName) VALUES (@folderName)");

command.Parameters.Add(new MySqlParameter("@folderName", DbType.NVarChar, 255, folderName));

command.ExecuteNonQuery();

Also, folderName must be a unicode column (NCHAR or NVARCHAR).

RQDQ
  • 15,461
  • 2
  • 32
  • 59
0

It seems to be passed via querystring as the wrong value. Where is the value coming from? That seems to be where the problem is being created.

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159