Database does not recognize text with special characters even in UTF-8

Asked

Viewed 174 times

1

I’ve made a lot of changes to the code and I’ve researched several sites and nothing has solved my problem!

HTML code:

<div class="w3-container w3-padding-32" id="about">
<h3 class="w3-border-bottom w3-border-light-grey w3-padding-16">Sobre o site</h3>
<p>
    <?php
            $sql_sobre = "SELECT sobre_home FROM campos";
            $sql_sobrequery = mysqli_query($conn, $sql_sobre) or die (mysqli_error($conn));
            $row = mysqli_fetch_row($sql_sobrequery);
            $decoded = utf8_encode($row[0]);
            echo $decoded;
    ?>
</p><div id="edicao" hidden><form method="POST" action="confirmarEdicao.php"><textarea class="textboxEditar" name="edicaoSobreHome"><?php echo $decoded ?></textarea> <input class="buttonEditar" type="submit" value="Confirmar"></form></div>

PHP code:

    session_start();
    $servername = "localhost";
    $username = "root";
    $password = "";
    $db = "aeac";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $db);

    // Check connection
    if (!$conn) {
        die("Connection failed: " .  mysqli_connect_error);
    }
    else{
      if (isset($_POST['edicaoSobreHome'])){
      $editar = $_POST['edicaoSobreHome'];
      $editar_encoded = utf8_encode($editar);
      $query = "UPDATE campos SET sobre_home = '$editar_encoded'";
      $result = mysqli_query($conn, $query);
    }
  }

The purpose of this div is to edit the text on the site itself through a hidden textarea that appears after clicking a button with a JS function.

My problem here is that by inserting special characters as letters with accents, the text in the database does not recognize the characters and returns text as the following:

  "pode encontrar informação sobre as"
  • How to header your html?

  • <meta http-equiv="Content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-Scale=1">

  • What table charset in the database?

  • utf8_spanish_ci

  • 1

    Dear(a) Redcandy do not use UTF8_DECODE OR UTF8_ENCODE, follow the instructions: https://answall.com/a/43205/3635

2 answers

1


Just remove the utf8_encode in update and select.

Put in the bank connection mysqli_set_charset($conn, "utf8") to define the mysqli charset

  $conn = mysqli_connect($servername, $username, $password, $db);
  mysqli_set_charset($conn, "utf8");

      $editar = $_POST['edicaoSobreHome'];
      $editar_encoded = utf8_encode($editar);  //<-- remover essa linha
      $query = "UPDATE campos SET sobre_home = '$editar'"; //<-- aqui $editar

      var_dump($query); //<-- aqui tem que estar correto
      $result = mysqli_query($conn, $query);
  • Please avoid long discussions in the comments; your talk was moved to the chat

-1

$pdf->load_html(utf8_decode($html)); Remove "utf8_decode" from the code and leave it this way---->>>>>>> $pdf->load_html($html);

Then you’ll pick up special characters at will.

Browser other questions tagged

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