5

I have a problem with my mysql insert statement. I have a form which is submitting utf-8 characters correctly to the insert file (i've checked the POST vars).

Now when i look after the INSERT in my DB, there are no umlauts, but question marks.

The error must be right before the insert statement.

If i output (manually entered) content from my DB, umlauts are correctly displayed.

// echo $_POST["title"];
// outputs correctly with special chars: "Some german title with umlaute ä ö ü"

mysql_query("INSERT INTO videos (youtube_hash, title, description, category, created) VALUES ('".mysql_real_escape_string($_POST["hash"])."', '".mysql_real_escape_string($_POST["title"])."', '".mysql_real_escape_string($_POST["desc"])."', '".mysql_real_escape_string($_POST["cat"])."', '".time()."')") or die(mysql_error());

// database entry looks like this: "Some german title with umlaute ? ? ?"

I hope anyone can help me out in this :)

EDIT:

htmlentities() did the job!

DonCroce
  • 475
  • 3
  • 8
  • 17

3 Answers3

4

When you insert your data from php into your mysql database try wrapping your string data using utf8_decode();

utf8_decode(string)

As the php manual says this converts utf8 to ISO-8859-1 (latin1);

You might also want to experiment with the iconv() function. This gives you more choice of your input encoding and desired output encoding

string iconv ( string $in_charset , string $out_charset , string $str )

If that still doesn't help try change the Collation on the Mysql table column that you are inserting into, to latin1_swedish_ci.

Nischal Bachu
  • 127
  • 1
  • 13
1

Using HTML entities isn't the nicest solution to your problem. Ideally you should be storing data in the database and only using htmlentities() for display purposes. What if you ever wanted to display/export this data in some other way than in HTML.

I'd recommend reading up on character set handling in PHP/MySQL. Here's an article I wrote recently: How to Avoid Character Encoding Problems in PHP

Post up again if you're still having problems.

James C
  • 14,047
  • 1
  • 34
  • 43
0

Try to change the set of character of MySQL DB from Latin1 to UNICODE.

Overflowh
  • 1,103
  • 6
  • 18
  • 40
  • You say that when you extract the umlaute from DB manually it works. Maybe it could be just an error of display given by your system (or browser). Try to change the set of character of the HTML page. – Overflowh Jul 04 '11 at 10:47