0

I have a form that reads some data. And a php file that sends this data to the database. All of the html files and php file are UTF-8. When the form reads the data, I have a check with echo right before the INSERT statement. The echo displays the words correctly. But when it is sent to database, it arrives with broken characters.

For example: I send the word "Būve". echo prints it exactly like that. But in the database I get "BÅ«ve". I have both of these right after connection as well.

mysql_query("SET NAMES UTF8");
mysql_query("SET CHARACTER SET utf8");

PHP code:

echo $_POST[pasutitajs];

$sql = "INSERT into ligumitable (nrpk, ligums, pasutitajs, parkstisanasdatums, summa,)  VALUES ('$_POST[nrpk]', '$_POST[ligums]', '$_POST[pasutitajs]', '$_POST[parkstisanasdatums]', '$_POST[summa]')";

Tables in the database are utf8 as well, if I unput the data through phpmyadmin, using these characters, everything is fine. It's the INSERT that breaks it. What could be wrong?

EDIT:

This is funny. Thing is, i am not the only one working on the code. And now that I checked it, my buddy used "$conn->query($sql)" and not "mysql_query($sql)". No idea why. I changed it to a regular mysql_query and it works. Thanks everyone, sorry for bothering you with such a simple thing.

  • BTW: You should use quotes to access your array values. (e.g. `$_POST['summa']` instead of `$_POST[summa]`. – mario.van.zadel Mar 01 '16 at 09:19
  • use database collection as utf8_general_ci – Sahil Manchal Mar 01 '16 at 09:20
  • Stupid point to rule out: you *are* using `mysql_query` to execute `$sql`, right? Do look here for a step by step checklist: [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/). – deceze Mar 01 '16 at 09:25
  • This is funny. Thing is, i am not the only one working on the code. And now that I checked it, my buddy used "$conn->query($sql)" and not "mysql_query($sql)". No idea why. I changed it to a regular mysql_query and it works. Thanks everyone, sorry for bothering you with such a simple thing. – user3242141 Mar 01 '16 at 09:33
  • Do not use the `mysql_*` interface, switch to `mysqli_*`. – Rick James Mar 01 '16 at 19:59

2 Answers2

-1

You can maybe fix it like this:

$sql = utf8_decode("INSERT into ligumitable (nrpk, ligums, pasutitajs, parkstisanasdatums, summa,)  VALUES ('$_POST[nrpk]', '$_POST[ligums]', '$_POST[pasutitajs]', '$_POST[parkstisanasdatums]', '$_POST[summa]')");
WowThatsLoud
  • 115
  • 7
  • Hmm.. Just tried it, now it puts question marks instead of these symbols. Like this: "B?ve". Could it it be a server-side issue? – user3242141 Mar 01 '16 at 09:25
  • You can run a replace function over the string, build up a PHP class with the characters that give issues and the HTML ascii code to replace them. That should fix it – WowThatsLoud Mar 01 '16 at 09:26
  • God no, don't do that. "ū" isn't supported in the ISO-8859-1 encoding, which is why `utf8_decode` destroys it and also otherwise doesn't help. – deceze Mar 01 '16 at 09:28
  • Don't use utf8_decode, it just adds to the mess. – Rick James Mar 01 '16 at 20:00
-1

before inserting data into database write this two line:

  $str="set names UTF8";
  mysql_query($con,$str);

in $con write your connection codes.

m.moghadam
  • 82
  • 8