Re: inserting utf8 from PHP to MySQL makes text unreadable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dave wrote:
PHP General,

The Situation:
I am creating a form for users to enter text into a MySQL 3.23 database. The text is often in Japanese, encoded in utf-8 format.


The Problem:
When the utf-8 encoded text is inserted into the database, it becomes random ASCII gibberish.


What I've Tried So Far:
I've noticed that it is possible to save utf-8 encoded text in the database. When I insert Japanese text into the database using phpMyAdmin, it stores okay, and I can access the text via PHP for placement on web pages. So my speculation is that there is something wrong with my script.
My search on the web for information has turned up indication that it should be possible to simply store utf-8 text without the need for modifications. Some places suggest that it might be best to store the Japanese text as binary. But since I can enter utf-8 text into fields and store them as text successfully in phpMyAdmin, I am sure there must be a way for getting around binary storage. I'd like to avoid binary storage if possible, for a variety of reasons.

The difference between text and binary in mysql is that text column type takes into account character set for stuff like case insensitive comparition and the likes. Character set is set per server basis in mysql < 4.1, so if your mysql is set to use iso-8859-1, it's better to store utf-8 in binary to get rid of the transformations.



The Question:
How can I preserve the text so that it does not become garbled when written to the database?


For Reference:
Here is the form and the PHP script that it accesses. $showData is obtained by querying the database and seeing what is already stored there.

The main part is missing here - <head> section of your page. It must contain


<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

else it will default to iso-8859-1.


<form enctype="multipart/form-data" action="" method="POST">
<p>English (256 characters)</p>
<p><textarea name="introE" rows="5" cols="50"><?php echo $showData['introE'];?></textarea></p>
<p>(Japanese)(256 Characters)</p>
<p><textarea name="introJ" rows="5" cols="50"><?php echo $showData['introJ'];?></textarea></p>

Use htmlspecialchars() to display $showData['introJ']

<p><input type="submit" name="submit" value="submit" /></p>
<?php
if (isset ($HTTP_POST_VARS['introE']) || isset ($HTTP_POST_VARS['introJ']))
{
$updateQuery = "UPDATE events SET introE='" . $HTTP_POST_VARS['introE'] . "', introJ='" . $HTTP_POST_VARS['introJ'] . "' WHERE eventid = " . $show;
$updateResult = mysql_query($updateQuery);
}
?>
</form>


   Any help much appreciated.


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux