How to handle special characters when entering data into a php+mysql database

Asked

Viewed 24 times

-1

So, guys, I’m trying to input some data from a log screen into the database, but when I input it, the special characters click into the database. Follow the code in php:


<?php
header('Content-Type: text/html; charset=utf-8');
$fullName = $_POST["name"];
$senha = $_POST["password"];
$confirmSenha = $_POST["confirmPassword"];
$rua = $_POST["endereco"];
$numero = $_POST["numero"];
$bairro =  $_POST["bairro"];
$conn = mysqli_connect("localhost", "usuario", "senha", "teste");
if (!$conn) {
    echo "Failed to connect with MySQL" . mysqli_connect_errno();
}
if ($senha == $confirmSenha) {
    $sql = "INSERT INTO usuario (fullName,senha,rua,numero,bairro) VALUES('$fullName','$senha','$rua','$numero','$bairro')";
    if (mysqli_query($conn, $sql)) {
        echo ("<script>alert('Registrado com sucesso!!');</script>");
    } else {
        echo "Erro";
    }
} else {
    echo ("<script>alert('Senhas não são iguais.');window.location.href('telaRegistro.html');</script>");
}

And the bank:Banco de dados usado

In this case, the id user 3 and 5 were manually inserted by a query in the heidisql itself, the other ones that are bugged are inserted through the code.

1 answer

0

Hello, how are you?

So, let’s do the following, let’s inform you what the charset Mysql must use for the current connection, we will do it as follows:

mysql_set_charset("utf8");

After your connection, enter the code above. See you soon...

  • Thanks, buddy! Solved the bug.

  • Use the tools to thank, there is no need to write thank you, however, for nothing!

Browser other questions tagged

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