0

adress.js

function addAdress(){
var s = document.getElementById('addStreetName').value;
var sn = document.getElementById('addStreetNumber').value;
var sl = document.getElementById('addStreetLittera').value;
var sz = document.getElementById('addZipCode').value;
var sa = document.getElementById('addAreaCode').value;

$.ajax({
    url: 'db/addAdress.inc.php',
    type:'POST',
    dataType: 'json',
    data: { 
        addStreetName: s,
        addStreetNumber: sn,
        addStreetLittera: sl,
        addZipCode: sz,
        addAreaCode: sa
    },
    success: function(output_string){
        $('#addAdressResultBox').append(output_string);
    },
    error: function (xhr, ajaxOptions, thrownError) {
    alert(xhr.status);
    alert(thrownError);
  }
});
}

addAdress.inc.php

include('../inc/conn.inc.php');

$query = "INSERT INTO comhem_profiler_adresses (comhem_profiler_adresses_street, comhem_profiler_adresses_street_number, comhem_profiler_adresses_littera, comhem_profiler_adresses_zip_code, comhem_profiler_adresses_area_code)
VALUES ('" . $_POST['addStreetName'] . "', '" . $_POST['addStreetNumber'] . "', '" . $_POST['addStreetLittera'] . "', '" . $_POST['addZipCode'] . "', '" . $_POST['addAreaCode'] . "'";
$addAdress = mysql_query($query) or die(mysql_error()); 
mysql_close();

In this script I want to add adresses to the database. But when they end up in the database the special characters å ä ö en up as completly other charcters. Sörbyvägen ends up like Sörbyvägen. I do not know where to look the change this.

It does not seem to be the database as i took a script to change these to their html counterparts. Sörbyvägen still appeard in the database but with the html counterparts. And tried htmlspecialchars(). Still appeared as Sörbyvägen in the database.

Any idea?

Nicklas
  • 57
  • 1
  • 6
  • Side note: please Google for Little Bobby Tables. – Álvaro González Nov 13 '12 at 09:32
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Nov 13 '12 at 09:36

2 Answers2

1

Make sure your table's default-character-set is utf8 and default-collation is utf8_general_ci

Also make sure after connecting to MySQL you use mysql_set_charset("UTF8", $connection);

JHS
  • 7,761
  • 2
  • 29
  • 53
  • 1
    Without forgetting `header("Content-Type: text/html; charset=utf-8");` or `AddDefaultCharset UTF-8` in apache config – Esailija Nov 13 '12 at 09:46
0

U need to correctly encode such character to UTF-8 as well as setting up your database to work with UTF-8, thenit will work like a charm. Also you need to know what was the base encoding to convert it correctly to utf-8

VicoMan
  • 289
  • 2
  • 13