Punk Rock Geek Posted May 26, 2009 Share Posted May 26, 2009 I have a form where I wish for users to insert their city. Some cities in the world are going to have foreign characters such as è in their name, and I'm wondering how I can make it so that regardless of what they type, it will be inserted into the database correctly. Currently, entering the name "Trois-Rivières" returns "Trois-Rivières" for example. Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/ Share on other sites More sharing options...
redarrow Posted May 26, 2009 Share Posted May 26, 2009 header('Content-Type: text/html; charset=utf-8'); might be this not sure Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-842688 Share on other sites More sharing options...
Punk Rock Geek Posted May 26, 2009 Author Share Posted May 26, 2009 That would apply to the entire file though? I'm actually modifying existing code. (Invision Power Board, to be be exact.) I'm worried that doing that will mess up something that they've written. Is that the only solution? Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-842704 Share on other sites More sharing options...
redarrow Posted May 26, 2009 Share Posted May 26, 2009 all i can think off, setting the value of the inserted word of the database value. making the 1 set field enter the unrecognized letter. <? mysql_query("insert table_name set field = _utf8'value'"); //'value' came from post variable ?> Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-842712 Share on other sites More sharing options...
c_shelswell Posted May 26, 2009 Share Posted May 26, 2009 i've just been doing this. Use utf-8 character encoding in your metatags. Then when you add something to the database use htmlentities('your funny text'); then when you want to display it: html_entity_decode($var); I had to do it that way anyway as the pages i'm using have a variety of strange and funny characters. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-842721 Share on other sites More sharing options...
Punk Rock Geek Posted May 27, 2009 Author Share Posted May 27, 2009 all i can think off, setting the value of the inserted word of the database value. making the 1 set field enter the unrecognized letter. <? mysql_query("insert table_name set field = _utf8'value'"); //'value' came from post variable ?> Thanks, but this did not work. Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-842921 Share on other sites More sharing options...
Punk Rock Geek Posted May 27, 2009 Author Share Posted May 27, 2009 i've just been doing this. Use utf-8 character encoding in your metatags. Then when you add something to the database use htmlentities('your funny text'); then when you want to display it: html_entity_decode($var); I had to do it that way anyway as the pages i'm using have a variety of strange and funny characters. Hope that helps. I don't understand. So if I want to add it to the database, I would do this? //$variable = "Trois-Rivières"; $variable2 = htmlentities('$variable'); And then insert $variable2 to the database? Because, it is just inserting the word "$variable" and not the actual content of it. Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-842925 Share on other sites More sharing options...
anupamsaha Posted May 27, 2009 Share Posted May 27, 2009 Please remove the single quote character from $variable. //$variable = "Trois-Rivières"; $variable2 = htmlentities($variable); Hope this will help. Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-842931 Share on other sites More sharing options...
Daniel0 Posted May 27, 2009 Share Posted May 27, 2009 You'll need the table's collation to be UTF-8, you'll need to set the connection to UTF-8, you'll need to tell the browser that it's UTF-8 and when using functions like htmlentities you'll have to tell it that it is UTF-8. Generally, you need to make sure you use the same character set throughout the entire process. Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-843042 Share on other sites More sharing options...
Punk Rock Geek Posted May 27, 2009 Author Share Posted May 27, 2009 I removed the single quotes, and it's still adding to the database incorrectly. While before it added characters like è, it is not adding stuff like è You'll need the table's collation to be UTF-8, you'll need to set the connection to UTF-8, you'll need to tell the browser that it's UTF-8 and when using functions like htmlentities you'll have to tell it that it is UTF-8. Generally, you need to make sure you use the same character set throughout the entire process. Okay, any UTF-8 specifically? Currently, the table's collation is set to utf8_unicode_ci. I also have the field set to that. I'm not familiar with how to set the other three (connection, browser, and htmlentities) to UTF-8 as well. Please explain? Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-843323 Share on other sites More sharing options...
Daniel0 Posted May 27, 2009 Share Posted May 27, 2009 Well, you can set the MySQL connection to UTF-8 by running this query after connecting: SET NAMES utf8; You set it in the browser like this: header('Content-type: text/html; charset=utf-8'); And as described on htmlentities's manual page, you do something like this: htmlentities($string, ENT_COMPAT, 'UTF-8'); Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-843325 Share on other sites More sharing options...
Punk Rock Geek Posted May 27, 2009 Author Share Posted May 27, 2009 Well, you can set the MySQL connection to UTF-8 by running this query after connecting: SET NAMES utf8; You set it in the browser like this: header('Content-type: text/html; charset=utf-8'); And as described on htmlentities's manual page, you do something like this: htmlentities($string, ENT_COMPAT, 'UTF-8'); Cool, I only changed the last one (like I said, I can't change the others because I don't want to mess up something else in the IPB software.) The è now enters the database as a è This isn't exactly what I was going for, but it seems to work just as well. When I call the information from the database, it automatically converts it to an è, as far as I can tell. Unless doing it this way would create some additional consequence that I'm not even considering? Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-843346 Share on other sites More sharing options...
Daniel0 Posted May 27, 2009 Share Posted May 27, 2009 Well, in my opinion you shouldn't insert HTML escaped data into the database. If you're having trouble with IPB you should contact their support department. Especially considering you are actually paying them. Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-843356 Share on other sites More sharing options...
Punk Rock Geek Posted May 27, 2009 Author Share Posted May 27, 2009 Well, in my opinion you shouldn't insert HTML escaped data into the database. If you're having trouble with IPB you should contact their support department. Especially considering you are actually paying them. Why would it be bad to do this, in your opinion? As far as IPB goes, I'm modding their product to do things it wasn't originally intended to do. They have no obligation to help me. Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-843365 Share on other sites More sharing options...
Daniel0 Posted May 27, 2009 Share Posted May 27, 2009 Because I believe you should store the raw data and format/filter it at the moment you need it and not before. Quote Link to comment https://forums.phpfreaks.com/topic/159775-add-foreign-characters-like-%C3%A8-to-a-database/#findComment-843376 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.