My application does not save accented data correctly

2

I'm developing a web application with ExtJS 4 and PHP. I'm having trouble making 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 in UTF-8 header, my HTML too, I'm saving my UTF-8 files, that is, everything is with the same encoding.

Below is the line of code where I make 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
    )
));
?>
    
asked by anonymous 28.11.2014 / 12:52

1 answer

1

The transport of the MySQL data from the PHP library to the driver is likely to be encoded. I mean, the page has the correct encoding, the database is writing things to UTF-8, but what comes to it may not be UTF-8. To make sure of this, 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. Should work if everything is UTF-8 as you say.

EDIT:

Actually the problem was the stripslashes function that removed the bar from the Unicode encoding (Format: \ u0000) placed by JSON on POST, causing the encoding to become uncharacteristic. I will leave the original answer as it may help in cases of accent problems, but I know that is not the right answer in the case of the question.     

28.11.2014 / 14:13