WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a simple Wordpress page that takes input from users and store into a database. I am able to get the values and update the database properly. However, whenever I try to enter some non-English text, it gets converted to something like '?????????'. But the issue is only with my page. If I use the Wordpress comments form, the text is stored in the database correctly.

My complete code is available at http://pastebin.com/MDLJBXpQ

Please help.

share|improve this question

In your wp-config.php set the encoding constants correctly:

define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', 'utf8_general_ci' );

And add an accept-charset attribute to your form:

    action="<?php bloginfo('url'); ?>/message/" 
share|improve this answer
Thank you for the help. I tried your suggestions and still its not working. – Albin Joseph Jun 9 '12 at 0:01

Since this is a custom table, make sure the table's encoding is et to UTF-8. If you created the table manually, it's possible that it got created using a different encoding, even if you explicitly declared a UTF-8 encoding in your config file. (That is to say, I don't think WordPress will automatically convert your tables based on the config setting.)

Also, you should really, really be using WPDB's prepare method to parameterize your queries. This helps prevent SQL injection. So your query should look something like:

$result = $wpdb->query( $wpdb->prepare( "INSERT INTO message (username, message) VALUES (%s, %s)", $name, $message) );
share|improve this answer
My table is using utf-8 encoding only. Thank you for your tip on prepare method. I will change my code. – Albin Joseph Jun 9 '12 at 0:06

This happens because your Charset and collation are not utf8, utf8_general_ci.

You have a few options in order to fix this: 1) (recommended and simplest) Use phpMyAdmin to change your collation, this is a fairly simple task. Depending on the version your using here's how for ver 3.4.5: When logging into phpmyadmin on the left panel you will see your databases, select the one you used for wordpress. Then you will see all the tables in that same panel, select the table you would like to edit. Then you can click Operations on the top menu and in Table options change the collation.

2) You can set a new default for your entire database.

3) Recreate your table setting the default Charset and Collation here's how:

$sql = "CREATE TABLE $my_table_name (
message TEXT NOT NULL,
)DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

I suggest if your creating a theme or plugin you set a default charset and collation for your table.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.