Solution 1 :

I had to set the charset at the end just before closing the connection.

Problem :

I’m using php to insert data from inputs into MySQL database. The problem is, it shows up in phpmyadmin as foreign characters. I’ve already set my database to utf8_hungarian_ci as you can see here:

I’m using utf 8 charset in my form:

header('Content-type: text/html; charset=utf-8');

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Add Records Form</title>
<form action="insert.php" method="post">
<label for="firstName">Név:</label>
<input type="text" name="name" id="firstName">
<label for="lastName">Foglalkozás:</label>
<input type="text" name="job" id="lastName">
<label for="emailAddress">Email:</label>
<input type="text" name="email" id="emailAddress">
<input type="submit" value="Add Records">

and I’m also using mysqli_set_charset in my sql connector:

$link = mysqli_connect("localhost", "root", "", "reg");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());

// Escape user inputs for security
$name = mysqli_real_escape_string($link, $_REQUEST['name']);
$job = mysqli_real_escape_string($link, $_REQUEST['job']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);

// attempt insert query execution
$sql = "INSERT INTO cards (name, job, email) VALUES ('$name', '$job', '$email')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

// close connection
mysqli_set_charset($link, "utf8");

but I still get this result on my page with characters like ő, ú, ű, á, etc.

Edit: I’m also using this header on my index page:

header('Content-type: text/html; charset=utf-8');


Comment posted by N3x

I haven’t used it yet but are you sure you have to set the charset at the ende just before closing the connection?

Comment posted by Mower

lol I’m so stupid. Just set it after connection and it’s working.. Thanks mate.