0

This Json data "Åland Islands", This is written in the database view - "Ã…land Islands"? Please, Help me find the wrong. `

<?php
$link = mysqli_connect('localhost','root','password','days');
$string = file_get_contents('data.json', true);
$json_a = json_decode($string, true);
foreach($json_a as $arr)
{
    $name = $arr['countryName'];
    $query = "INSERT INTO countries(name) VALUES ('$name');";
    mysqli_query($link,$query);
    $country_id = mysqli_insert_id($link);
    foreach ($arr['regions'] as $region)
    {
        $region_name = $region['name'];
        $query = "INSERT INTO regions(country_id,name,created_at,updated_at) VALUES ($country_id,'$region_name',NOW(),NOW());";
        mysqli_query($link,$query);
    }
}
?>`
  • Please read this **[very informative q/a about UTF8, right here on SO!!!](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through)** and review your solution. A must read, really. – YvesLeBorg Jul 07 '18 at 11:28
  • Your code has SQL injection vulnerabilities. SQL injection doesn't come only from user input. It can come from reading files too. [Learn to use query parameters](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) to avoid these vulnerabilities. – Bill Karwin Jul 07 '18 at 17:03
  • Edit your question and add the output of `SHOW CREATE TABLE countries` and `SHOW CREATE TABLE regions`. – Bill Karwin Jul 07 '18 at 17:04

1 Answers1

0

What is the character set & collation for the column 'name' and what data type is it? My guess is that your are not using a JSON data type column and/or it is not UTF8MB4.

Dave Stokes
  • 775
  • 4
  • 10