Wrong encoding in Mysql when I enter via AJAX

Asked

Viewed 107 times

-1

I’m having trouble finding a solution to my problem. Briefly:

I send data to a table, using a php file that does querry, and an ajax call that sends all the values I need to the php file. When it is inserted in the table the accents of the words are all crazy. If I add entries to the table using phpmyadmin this error does not happen. How can I resolve?

  • Call AJAX:

    pageurl = 'http://estradasegura.pt/exames/testesDB.php';
                        //para consultar mais opcoes possiveis numa chamada ajax
                        //http://api.jquery.com/jQuery.ajax/
                        $.ajax({
    
                            //url da pagina
                            url: pageurl,
                            //parametros a passar
                            data: dadosajax,
                            //tipo: POST ou GET
                            type: 'POST',
                            //cache
                            cache: false,
    
  • PHP file connecting to Mysql:

    ini_set('display_errors', true); error_reporting(E_ALL);
    
    $dbname = 'nomedabasededados';
    $username = 'meuusername';
    $password = 'ahahahaha';
    $servername = 'localhost';
    
    $pergunta = $_POST['pergunta']; 
    $resA = $_POST['resA'];   
    $resB = $_POST['resB'];
    $resC = $_POST['resC'];  
    $resD = $_POST['resD'];  
    $resE = $_POST['resE'];  
    $acertaram = intval($_POST['acertaram']); 
    $falharam = intval($_POST['falharam']);
    $dificuldade = intval($_POST['dificuldade']); 
    $resposta = intval($_POST['resposta']);  
    $imgSrc = $_POST['imgSrc'];
    
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    
    $sql = " INSERT INTO perguntas ( pergunta, resA, resB, resC, resD, resE, acertaram, falharam, dificuldade, resposta, imgSrc )  VALUES ( ";
    
    $sql .= "'$pergunta', '$resA', '$resB', '$resC', '$resD', '$resE', '$acertaram', '$falharam', '$dificuldade', '$resposta', '$imgSrc' )" ;
    
     $result = mysqli_query($conn, $sql);
    
    $conn->close();
    
    
    
    ?>
    

  • The table is in UTF8 ?

  • is in utf8_general_ci

  • It may be three things, I don’t touch mysqli (PDO use), but my DSN in PDO is utf8. Or it may be your header('content-type text/html charset=utf-8'). It may also be the file, if your file is being saved as utf8

  • header('content-type text/html charset=utf-8') I used this before asking, but as it did not change anything I took from php. As for the file...I don’t know what it is because I made my first code in php and mysql yesterday and only had a few hours to figure out how to use values from/for the database

  • You use a Notepad++ ?

  • yes, to edit PHP I use Notepad++, to edit html and that I use Brackets (Filezilla opens php with Notepad++ by default and was not there to change)

Show 1 more comment

3 answers

0

Solution 1

Solução 1

Solvency 2

Solução 2

Ps: I use Sublime Text 3.
Preferences -> Settings - Syntax Specifics -> //in JSON does so:
"show_encoding": true

Solution 3 - Notepad++

notepad++ encoding

0

You could use utf8_encode in the data you need for characters in the post variable assignment.

http://php.net/manual/en/function.utf8-encode.php.

You can also try to modify the charset in your html

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

And in your php

header( 'Content-Type: text/html; charset=utf-8' );
  • I used all three and everything’s still the same.

  • Take a look at the bank, at the table more carefully. Try to change the collate to utf8_unicode_ci. There is a discussion here (https://forums.mysql.com/read.php?103,187048,188748), saying that utf8_general_ci , is simpler, gives some problems...

  • I already managed to solve the problem, see the answer I put. is very similar to your suggestion of encode, only that it is another function for the same purpose.

  • Your solution converted the string to ISO...

  • yes, but it worked :’D

0

Solution found:

The solution I found that worked for my case is as follows::

$pergunta = iconv("UTF-8", "ISO-8859-1", $_POST['pergunta']);

With this change the strings are saved in the database without any type of error of the characters. For those who want to know more about iconv() can visit this link.

  • By the command you gave your string came out of type "ISO-8859-1"... Gives a utf8_decode and see if the result is the same...

  • confirm that it works in exactly the same way

  • So the problem is the reverse! D

Browser other questions tagged

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