0

I have a function that adds words in my database and for some reason it's putting them in all weird.

Here is my function :

function change_team($picname, $text){
    include 'dbconnector.php';
    $conn->query("UPDATE team SET  tea_photo = '$picname', tea_text = $text WHERE tea_id = 1"); 
}

Then, I use a post form to put it in :

change_team($_POST['lala'], $_POST['text']);

$_POST['text'] = éÀéÀ works when I echo it before entering it into database, but in the database it does this: Éà É

I've tried everything, I've put my table as uft8_general_ci but it doesn't seem like it fixes anything.

Nick Fury
  • 1,313
  • 3
  • 13
  • 23
Yann Chabot
  • 4,789
  • 3
  • 39
  • 56

1 Answers1

0

In your dbconnector.php, do:

$conn->set_charset("utf8");

After connecting and selecting database. This will set the transmission encoding to UTF-8. (The table/column/db character set is just storage encoding).

Esailija
  • 138,174
  • 23
  • 272
  • 326
  • Tried, doesnt solve my problem :( – Yann Chabot Apr 19 '13 at 19:47
  • If you are not at least seeing a different outcome, then you didn't apply it correctly somehow – Esailija Apr 19 '13 at 19:50
  • The datas are perfect until it comes to entering it into the database, which I dont understand because I've done everything everybody is suggesting everywhere... it just doesnt seem to work out – Yann Chabot Apr 19 '13 at 20:04
  • In fact, it can print UTF8 really well, if I enter then in my database manualy using SQL, it just seem that this query doesnt works : $conn->query("INSERT INTO categories VALUES(DEFAULT, '$name')"); any tought ? – Yann Chabot Apr 19 '13 at 20:12
  • @YannChabot I mean the command I suggested must at least give you different results. Where does `$name` come from? – Esailija Apr 19 '13 at 20:18
  • its a variable in the function ! No, sadly its still the same results, in fact, the command makes an error with my server... – Yann Chabot Apr 19 '13 at 20:19
  • OMG ! Found it !! In fact, you're right ! It made it work out... I had to do UTF8_DECODE() then to make it work, double encoding maybe ? – Yann Chabot Apr 19 '13 at 20:23
  • @YannChabot I need to know where the data originates from. Is it literally typed in your php source file? Coming from browser POST? Coming from 3rd party web service? These all have different solutions. – Esailija Apr 19 '13 at 20:23
  • @YannChabot no, never use utf8_decode. – Esailija Apr 19 '13 at 20:23
  • Yes, its a function argument, $name is from a POST form – Yann Chabot Apr 19 '13 at 20:28
  • @YannChabot right. Then use `header("Content-Type: text/html; charset=utf-8");` to make browser send you forms in UTF-8. Get rid of any functions you have used trying to fix this (such as `utf8_decode`) and ensure you still have `$conn->set_charset("utf8");` – Esailija Apr 19 '13 at 20:30