I’m trying to insert text into a MySQL DataBase from a form using PHP.
Problem is, when I check the DataBase in phpmyadmin, I find that special characters like ü, ö, ä, è, é, à… are being corrupted, and I get things like üöäéèÃ.I’ve tried the following:
- Putting
accept-charset="latin1_swedish_ci"in the<form>tag - Setting
default_charset = "latin1_swedish_ci"in the php.ini file - Putting
mysqli_set_charset($dbc, 'latin1_swedish_ci');andmysql_query('SET NAMES latin1_swedish_ci');after themysqli_connect()function - Setting the collation of the DataBase, the table and the individual columns to
latin1_swedish_ci
I noticed that running the query directly in the phpmyadmin the values weren’t corrupted, so I suppose the error must be somewere in the PHP and/or the MySQL connection.
Also, I tried doing the same with utf-8_general_ci, and obtained same results.
It seems to be ignoring anything I do.
Advice and ideas welcome. Thanks guys,
Sean
I can not properly say if that is the cause of your problem, but in any case you can not put a Mysql specifier of charset and collation neither into the HTML form nor into PHP.ini.
Right now you use the MySQL value somewhat blindly and put it everywhere you assume it fit – without even knowing. Don’t do that. First of all decide which encoding you want to use and accept. From your question I would say that is latin1.
So configure HTML and PHP appropriately:
For the HTML form element. Note that there is no
latin1_swedish_ciin HTML, see the HTML docs.A next and similar point is in your php.ini setting. There doesn’t exists something like you put there. Change it to latin1 as well:
With these two settings corrected, the browser is aware what the encoding of the page is that contains the form, and the form even signals the browser the accepted encoding of the data passed back to the server.
So this should ensure that the data you have inside the PHP variables is latin1 encoded. You can then pass this data into your database if you properly configured the encoding of the database client, the database server, the encoding of the connection between the two and naturally the encoding of the data in the database as well.
However, the proper setup of the form is the very first step you need to do before you can ensure that the mysql connection isn’t wrangling stuff up.