My application does not save accented data correctly

Asked

Viewed 213 times

2

I’m developing a web application with Extjs 4 and PHP. I’m having trouble doing the INSERT in the database. For example, I registered the product "tea", but it saves "chu00e1". My entire database is in UTF-8, my PHP files are with header in UTF-8, my HTML too, I am saving my files in UTF-8, IE, everything is with the same encoding.

Below is the line of code where I do the INSERT.

<?php
//chama o arquivo de conexão com o bd
include("connect.php");

$info = $_POST['data'];

$data = json_decode(stripslashes($info));

$codigo = $data->codigo;
$nome = $data->nome;
$descricao = $data->descricao;
$quantidade = $data->quantidade;
$sigla = $data->sigla;


$query = sprintf("INSERT INTO produtos (codigo, nome, descricao, quantidade, sigla) values ('%s', '%s', '%s', '%d', '%s')",
    mysql_real_escape_string($codigo),
    mysql_real_escape_string($nome),
    mysql_real_escape_string($descricao),
    mysql_real_escape_string($quantidade),
    mysql_real_escape_string($sigla));

$rs = mysql_query($query);

echo json_encode(array(
    "success" => mysql_errno() == 0,
    "data" => array(
        "id" => mysql_insert_id(),
        "codigo" => $codigo,
        "nome" => $nome,
        "descricao" => $descricao,
        "quantidade" => $quantidade,
        "sigla" => $sigla
    )
));
?>
  • Read the documentation: utf8_encode

  • I had already looked there. I can’t use utf8_encode because it asks String as parameter and my data is in an array.

  • This does not interfere with anything, Voce is storing a string inside the array :) : $seuCampo= utf8_encode($data->seuCampo);

  • Actually it has to be like this: $string = $data->seuCampo;&#xA;$suaStringCodificada = utf8_encode($string);

  • It didn’t work out, man..

1 answer

1


It is likely that the transport of Mysql data from the PHP library to the driver is with another encoding. I mean, the page is with the right coding, the database is recording things in UTF-8, but what comes to it may not be UTF-8.
To make sure of that, in your connect.php, run the following query:

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conexao);  

This will prevent you from having to convert (use utf8_encode() or utf8_decode()) when recording or displaying. It should work if everything is in UTF-8 as you say.

EDIT:

Actually the problem was the function stripslashes that removes the Unicode encoding bar (Format: u0000) placed by JSON in the POST, causing the encoding to be out of character.
I will leave the original answer because it may help in cases of accentuation problems, but I know that is not the right answer in the case of the question.

  • I tried that, but it didn’t work :(

  • The coding of the table produtos is also in UTF8? Which UTF8 collar is being used?

  • Have you certified that the browser is rendering the page as UTF8? It can happen to send the headers in UTF8 and even then the browser render with another charset.

  • The browser is UTF8. In the products table the collate is utf8_general_ci.

  • I reread your question and the code seems to be a problem with JSON. JSON encodes UTF8 characters in u0000 format, and you’re pulling this backslash through the stripslashes, making UTF8 encoding by JSON invalid. Try removing stripslashes as you are making a mysql_real_escape_string and let it record with the bar to see what happens.

  • Dude, I did this before you answered my question, but you explained to me why taking off the stripslashes worked. there in the Google Developer tools Chrome, in the network part, the data is still being sent as shown above ( u0000). It is normal?

  • Yes, it is normal. It is an alternative syntax of Unicode to encode characters. Let it save like this, it is not wrong.

  • Aaah got it. Thanks for helping me, man!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.