3
I can’t save sentences or words with special characters ( ' & " ). My bank is like this:
ALTER DATABASE `bancodedados` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
All fields in utf8 and utf8_general_ci certinho... It simply does not save the entire record, but if I remove the special characters from the field (which is not the case), it saves the entire record!
I’m using PHP+Mysql... In PHP I use the header:
header('Content-Type: application/json; charset=utf-8');
Why data comes from a JSON request! I’ve tried with "utf8_decode()" too and nothing!
Query:
$matriz = json_decode ( json_encode ($_POST['dados']), true);
$itens = $matriz['results'];
foreach ($itens as $e ){
$name = preg_replace("/[^a-zA-Z0-9_]/", "", strtr($e["name"], "áàãâéêíóôõúüçñÁÀÃÂÉÊÍÓÔÕÚÜÇÑ ", "aaaaeeiooouucnAAAAEEIOOOUUCN "));
$SQL = "INSERT INTO tb_detalhes_empresa (place_id, nome_empresa, latitude, longitude, icone, scope, aberto_agora, reference, vicinity, harario_func_dias, foto_referencia, foto_height, foto_width, foto_atribuicoes, aberto_agora_periodos)VALUES('".utf8_decode($e["place_id"])."', '".$name."', '".$_POST['latitude']."', '".$_POST['longitude']."', '".utf8_decode($e["icon"])."', '', '', '".utf8_decode($e["reference"])."', '".utf8_decode($e["vicinity"])."', '', '', '', '', '', '');";
$query = mysqli_query($serve, $SQL); or die("erro".mysqli_error());
}
Return JSON (part):
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.870775,
"lng" : 151.199025
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/travel_agent-71.png",
"id" : "21a0b251c9b8392186142c798263e289fe45b4aa",
"name" : "Rhythmboat Cruises",
"opening_hours" : {
"open_now" : true
},
"photos" : [
{
"height" : 270,
"html_attributions" : [],
"photo_reference" : "CnRnAAAAF-LjFR1ZV93eawe1cU_3QNMCNmaGkowY7CnOf-kcNmPhNnPEG9W979jOuJJ1sGr75rhD5hqKzjD8vbMbSsRnq_Ni3ZIGfY6hKWmsOf3qHKJInkm4h55lzvLAXJVc-Rr4kI9O1tmIblblUpg2oqoq8RIQRMQJhFsTr5s9haxQ07EQHxoUO0ICubVFGYfJiMUPor1GnIWb5i8",
"width" : 519
}
],
You need to post more parts of your code, post the query that makes the data entry in the database.
– Dunga Cardoso
edited the post!!!
– Rodrigo
I suspect the characters are breaking the query, they need to be escaped. take a test, remove '&' and do a code treatment with addslashes, $given = addslashes($_POST['donor']) to escape the quotes.
– Dunga Cardoso
I think it should be
utf8_encode()
and notutf8_decode()
– Berriel
Dude, and worse than a complicated json array of the Google Places API, I’m posting a piece. But everything works perfectly, the problem is time to save data with special characters, records that do not have special, save normal!
– Rodrigo
Tried to
utf8_encode()
?– Berriel
I tried, and nothing:
– Rodrigo
Like I said, try to escape the characters with the , type " & ', these characters may be misinterpreted.
– Dunga Cardoso
These are the results: Regency Cafe </br> Specialty’s Cafe & Bakery </br> Montague’s Cafe </br> Freedom Cafe
– Rodrigo
Where it only shows 1° and the last
– Rodrigo
Great! It worked with "addslashes()"... Thanks Dunga
– Rodrigo