• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CodeIgniter messing up é's

#1
[eluser]IanMcQ[/eluser]
Hi,

I have a form and one of the fields contains an ”é” (accented e). My MySQL charset and collation is correct (utf8 and utf8_unicode_ci). Whenever I submit the form, it makes the field “Bon Appétit” to “Bon App” (it cuts it off starting with the é). However, I can manually go into phpMyAdmin and enter in “Bon Appétit” and it takes the data. I’m guessing that this is a CI encoding issue. What could be causing this?

Here’s my code (abbreviated):

Code:
function avatar_edit()
  {
      $this->load->helper(‘url’);
      $id = $this->uri->segment(3);
      if (!is_numeric($id))
      {
        exit;
      }

      // handle post request
      if (isset($_POST[‘edit_avatar’]))
      {
        $avatar_id = $_POST[‘avatar_id’];
        $name = $_POST[‘avatar_name’];
        $solution = $_POST[‘avatar_solution’];

          $edit_info = array(
                                  ...
                                  ‘avatar_name’ => $name,
                                  ‘avatar_solution’ => $solution,
                                );
          $this->db->where(‘avatar_id’, $avatar_id);
          $this->db->update(‘neoavatars_list’, $edit_info);
          ...
      }

      ...
  }

#2
[eluser]IanMcQ[/eluser]
Anybody?

#3
[eluser]Bogdan Tanase[/eluser]
Do you have xss_clean enabled globally? If so, try disabling it.

Otherwise, try to see where exactly your data becomes trimmed. echo the variables before you insert them in the database, see if the database functions cause the problem, or is somewhere else.

Also, have you checked your html encoding?

That is the line:
Code:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

#4
[eluser]Sumon[/eluser]
From PHP bug list: http://bugs.php.net/bug.php?id=19570&edit=2

So, use utf8_decode() and utf8_encode() function in your code.

Here is an example code i have written for you. i hope it's helpful for you

db_schema
Code:
CREATE TABLE `users` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL,
  `email` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`)
)

Controller test
Code:
function reg_user()
{
    $this->load->helper('form');
    if($_POST)
    {
        echo $_POST['username']."<br>";
        $this->load->model('test_model');
        $id = $this->test_model->insert_user($_POST);

        $user_data = $this->test_model->get_user($id);
        echo utf8_decode($user_data['name']);
    }
    $this->load->view('reg_user_view');
}

Model test_model
Code:
function insert_user()
{
    extract($_POST);
    $this->db->set('name', utf8_encode($username));
    $this->db->set('email', $email);
    $this->db->insert('users');
    return $this->db->insert_id();
}

view reg_user_view.php
Code:
&lt;?php echo form_open('test/reg_user'); ?&gt;
  username :&lt;input name="username" type="text" maxlength="255" size="60" value="" /&gt; <br />
  email :&lt;input name="email" type="text" maxlength="255" size="60" value="" /&gt; <br />
  &lt;input type="submit" name="submit" value="submit"&gt;
  &lt;/form&gt;

#5
[eluser]Dan Bowling[/eluser]
Another thing you might try is echoing print_r($_POST) to see if the data actually made it into the post array. It is probably just the encoding mode the browser is is in.

#6
[eluser]maesk[/eluser]
Did you put this in your config/database.php:

Code:
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

I use Unicode all the time (not just "é" but also German Umlaute like äöü, daggers and stuff) and it works without problems with CI. No need to use utf8_decode() and utf8_encode().

@Sumon: The PHP bug you linked to is from 2002 and called "Not all cyrillic symbols present in HTML_ENTITIES table". I don't see the connection to this problem.

#7
[eluser]maesk[/eluser]
Another thing when working with Unicode (outside of CodeIgniter) and MySQL that took me a long time to find out: you think you have set everything from MySQL charset to your HTML view file to UTF8 but still don't get the correct characters in your view, you have to set the MySQL connection to UTF8 like this:

Code:
mysql_query("SET NAMES 'utf8'");

#8
[eluser]Sumon[/eluser]
[quote author="maesk" date="1222867480"]Did you put this in your config/database.php:

Code:
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

I use Unicode all the time (not just "é" but also German Umlaute like äöü, daggers and stuff) and it works without problems with CI. No need to use utf8_decode() and utf8_encode().[/quote]

@maesk: yep my configuration file is exactly
Code:
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
i get "Bon Appétit" using $_POST['username']. but when i have tried to insert it into database. it doesn't work for
Code:
$this->db->set('name',$username);
But when i use
[/code]
$this->db->set('name', utf8_encode($username));
[code]
it works fine. Now, would you please rewrite my code correctly so that i can enter Bon Appétit in database.

#9
[eluser]maesk[/eluser]
[quote author="Sumon" date="1222911200"]

@maesk: yep my configuration file is exactly
Code:
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
[/quote]

I actually wondered about the OP's config but the problem seems to be the same.

[quote author="Sumon" date="1222911200"]
i get "Bon Appétit" using $_POST['username']. but when i have tried to insert it into database. it doesn't work for
Code:
$this->db->set('name',$username);
But when i use
[/code]
$this->db->set('name', utf8_encode($username));
[code]
it works fine.
[/quote]

Yeah, that's strange, doesn't happen in my install of CI.

[quote author="Sumon" date="1222911200"]
Now, would you please rewrite my code correctly so that i can enter Bon Appétit in database.[/quote]

Nope. Sorry, but I don't have time for this right now. All I'm saying is that I do not experience any problems with Unicode in CI and I don't use utf8_encode(). I just tried it with scaffolding and entered "Bon Appétit" and other accented characters into the DB and it works perfectly and scaffolding doesn't use utf8_encode() as far as I can see.

If you want to use utf8_encode() and it works for you that's fine with me!

#10
[eluser]Isuka[/eluser]
Check that your favorite editor encode your php files in UTF-8 too.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.