Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

3 Answers 3

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:

<form 
    name="frm" 
    method="post" 
    action="<?php bloginfo('url'); ?>/message/" 
    accept-charset='utf8'
>
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 (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
message TEXT NOT NULL,
)DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";

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

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

 
discard

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.