How to store Korean data with UTF-8 Encoding from Web Form

Posted on 2009-12-30
Last Modified: 2013-12-12
Hello all,

I'm caught in the mire of UTF-8 encoding, I guess.  I've got a database working well for a Korean client, that stores mostly regular English characters, (names, phone numbers, email, etc.), but also has one field for the Korean name of the person.

I've researched for several hours, trying to figure out how to properly handle this.  I see that the best way seems to be to keep things in UTF-8 encoding.  The database is relatively small, with mainly 4 tables, mainly two with most of the data.  I did not create it with UTF-8 encoding.  A few days ago, I changed a number of the fields to UTF-8 in the main table, using Navicat.  The Korean characters look right in that field.  

Yesterday I also was able to make one page display the Korean characters properly.  It took adding mysql_query("SET NAMES 'UTF8'"); in several places to make it happen.

My problem now is in a form where people edit a row of data.  The Korean characters show up properly in the input box when the data is pulled in, but when the update button is hit, the table now has garbage data in place of the Korean characters.  I'm assuming that the form is somehow not submitting the data in UTF-8 character encoding, though I may be wrong.  

I have

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

in place on the page.

I'll post the code for the form here as well.  Some of the code has been generated by Dreamweaver and ADDT.

Thanks in advance for any help on this frustrating issue.

<form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>" enctype="multipart/form-data" accept-encoding="UTF-8">
    <table cellpadding="2" cellspacing="0" class="KT_tngtable">
        <td valign="middle" class="KT_th"><label for="lastname">Last Name:</label></td>
        <td><input name="lastname" type="text" id="lastname" style="font-size:22px" value="<?php echo KT_escapeAttribute($row_rsindividuals['lastname']); ?>" size="35" />
          <?php echo $tNGs->displayFieldHint("lastname");?> <?php echo $tNGs->displayFieldError("individuals", "lastname"); ?></td>
        <td valign="middle" class="KT_th"><label for="firstname">First Name:</label></td>
        <td><input name="firstname" type="text" id="firstname" style="font-size:22px" value="<?php echo KT_escapeAttribute($row_rsindividuals['firstname']); ?>" size="35" />
          <?php echo $tNGs->displayFieldHint("firstname");?> <?php echo $tNGs->displayFieldError("individuals", "firstname"); ?></td>
        <td valign="middle" class="KT_th">Korean Name:</td>
        <td><input name="koreanname" type="text" id="koreanname" style="font-size:22px" value="<?php echo KT_escapeAttribute($row_rsindividuals['korean_name']); ?>" size="35" />
          <?php echo $tNGs->displayFieldHint("koreanname");?> <?php echo $tNGs->displayFieldError("individuals", "koreanname"); ?></td>
        <td valign="middle" class="KT_th"><label for="cell_phone">Cell Phone:</label></td>
        <td><input name="cell_phone" type="text" id="cell_phone" style="font-size:22px" value="<?php echo KT_escapeAttribute($row_rsindividuals['cell_phone']); ?>" size="32" />
          <?php echo $tNGs->displayFieldHint("cell_phone");?> <?php echo $tNGs->displayFieldError("individuals", "cell_phone"); ?></td>
        <td valign="middle" class="KT_th">Email:</td>
        <td><input name="email" type="text" id="email" style="font-size:22px" value="<?php echo KT_formatDate($row_rsindividuals['email']); ?>" size="32" />
          <?php echo $tNGs->displayFieldHint("email");?> <?php echo $tNGs->displayFieldError("individuals", "email"); ?></td>
        <td valign="middle" class="KT_th"><label for="birthdate">Birthdate:</label></td>
        <td><input name="birthdate" type="text" id="birthdate" style="font-size:22px" value="<?php echo KT_formatDate($row_rsindividuals['birthdate']); ?>" size="15" />
          <?php echo $tNGs->displayFieldHint("birthdate");?> <?php echo $tNGs->displayFieldError("individuals", "birthdate"); ?></td>
        <td valign="middle" class="KT_th"><label for="head">Head of Household:</label></td>
        <td><input  <?php if (!(strcmp(KT_escapeAttribute($row_rsindividuals['head']),"1"))) {echo "checked";} ?> type="checkbox" name="head" id="head" value="1" />
          <?php echo $tNGs->displayFieldError("individuals", "head"); ?></td>
      <tr class="KT_buttons">
        <td colspan="2"><input type="submit" name="KT_Update1" id="KT_Update1" value="Update record" /></td>
    <input type="hidden" name="family_id" id="family_id" value="<?php echo KT_escapeAttribute($row_rsindividuals['family_id']); ?>" />

Open in new window

Question by:ImagineItDigital
    LVL 14

    Expert Comment

    You did great job. I think the problem is not with the encoding of the page, rather it is with the file format. When you shift the encoding of the page to UTF-8 you need also to save the file as UNICODE or UTF-8, it is saved now as ASCII I guess. Using the text editor you can SAVE the file AS UTF-8 and I hope the problem fixes.

    Author Comment

    Thanks Profya,

    But I upload the files using Dreamweaver CS4, and it is set to upload in Unicode (UTF-8).  There is also a Unicode Normalization Form setting, currently set to C.  That may not apply - not sure what it is.

    So I'm still stuck.  Any other ideas?


    LVL 14

    Expert Comment

    Ok, just make sure that UTF-8 is written in the bottom right corner of the page, in the bar showing your HTML tags. This tells you whether you are using UTF-8 or not. You may need to reload your page to make sure that the file itself is saved as UTF-8. Or you can use any other simple text editor such as NotePad.

    Author Comment

    Yes it is written there in the bottom right.
    LVL 20

    Expert Comment

    where is the code that inserts from this form into the db?

    Author Comment

    // Make unified connection variable
    $conn_cornerstone = new KT_connection($cornerstone, $database_cornerstone);

    // Start trigger
    $formValidation = new tNG_FormValidation();
    // End trigger

    // Make an update transaction instance
    $upd_individuals = new tNG_update($conn_cornerstone);
    // Register triggers
    $upd_individuals->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Update1");
    $upd_individuals->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
    $upd_individuals->registerTrigger("END", "Trigger_Default_Redirect", 99, "index.php");
    // Add columns
    $upd_individuals->addColumn("family_id", "NUMERIC_TYPE", "POST", "family_id");
    $upd_individuals->addColumn("korean_name", "STRING_TYPE", "POST", "koreanname");
    $upd_individuals->addColumn("email", "STRING_TYPE", "POST", "email");
    $upd_individuals->addColumn("firstname", "STRING_TYPE", "POST", "firstname");
    $upd_individuals->addColumn("lastname", "STRING_TYPE", "POST", "lastname");
    $upd_individuals->addColumn("cell_phone", "STRING_TYPE", "POST", "cell_phone");
    $upd_individuals->addColumn("birthdate", "DATE_TYPE", "POST", "birthdate");
    $upd_individuals->addColumn("head", "CHECKBOX_1_0_TYPE", "POST", "head");
    $upd_individuals->setPrimaryKey("individual_id", "STRING_TYPE", "GET", "individual_id");

    // Execute all the registered transactions

    // Get the transaction recordset
    mysql_query("SET NAMES 'UTF8'");
    $rsindividuals = $tNGs->getRecordset("individuals");
    $row_rsindividuals = mysql_fetch_assoc($rsindividuals);
    $totalRows_rsindividuals = mysql_num_rows($rsindividuals);
    LVL 20

    Accepted Solution

    okay, can you post the actual query being submitted (it should be happening in $tNGs->executeTransactions();)?

    also, you might want to put the SET NAMES code above your executeTransactions();

    Author Comment

    That did it!  Just the placement of the Set Names.

    Thanks a million!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Problem to Popup 37 62
    WPF issue with Trigger 2 23
    Newbie CSS questions 4 14
    session variable - help 5 20
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
    In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now