Wrong encoding in MySQL when I enter through AJAX

-1

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

I send data to a table, using a php file that does the 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 occur. How can I solve?

  • AJAX call:

    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 linking 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();
    
    
    
    ?>
    
asked by anonymous 01.02.2017 / 20:12

3 answers

0

You could use utf8_encode in the data that needs characters in the assignment of the post variables.

link .

You can also try modifying 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' );
    
01.02.2017 / 21:35
0

Solution found:

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

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

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

    
02.02.2017 / 11:35
0

Solution 1

Solution2

Ps:IuseSublimeText3.
Preferences->Settings-SyntaxSpecify->//inJSONdoesthis:
"show_encoding": true

Solution 3 - Notepad ++

    
01.02.2017 / 20:37