通过javascript将数据发布到数据库会导致编码问题

时间:2012-10-18 12:35:59

标签: javascript php mysql encoding

如果还有其他类似的帖子,我很抱歉,但很难用问题形式化问题。所以我有一个包含Google地图的页面和两个可以在地图上添加额外信息的表单。一种形式运作良好,另一种形式不太好。我的语言是立陶宛语,所以我有一些特殊的字符,如“ąčęėįšųū”,提交后的第二种形式我在数据库中有错误的值,如“%u0117%u0161”。

enter image description here

在图像编号1中,此表格运行良好。数字2是产生编码问题的形式,数字3是提交表格编号2后的结果。

对于第二种表单,我使用了Google教程(https://developers.google.com/maps/articles/phpsqlinfo_v3),并编辑了一些小东西。

如果有人帮我解决这个问题,我将不胜感激。

Javascript代码:

 function initMap() {
 map = new google.maps.Map(document.getElementById("map"), {
 zoom: 0,
 center: new google.maps.LatLng(55.203953, 24.873047),
 mapTypeId: google.maps.MapTypeId.ROADMAP,
 mapTypeControl: true,
 panControl: true,
 mapTypeControlOptions: {
 style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
 },
 navigationControl: true,
 navigationControlOptions: {
 style: google.maps.NavigationControlStyle.SMALL
 }
 }); 


 var html = "<table>" +
                 "<tr><td>Pavadinimas:</td> <td><input type='text' name='name' id='name'/> </td> </tr>" +
                 "<tr><td>Miestas:</td> <td><input type='text' name='city' id='city'/></td> </tr>" +
                 "<tr><td>Adresas:</td> <td><input type='text' name='adress' id='adress'/></td> </tr>" +
                 "<tr><td>Apibudinimas:</td> <td><input type='text' name='description' id='description'/></td> </tr>" +
                 "<tr><td>Type:</td> <td><select id='type'>" +
                 "<option value='Baras' SELECTED>Baras</option>" +
                 "<option value='Restoranas'>Restoranas</option>" +
                 "<option value='Degalinė'>Degalinė</option>" +
                 "</select> </td></tr>" +
                 "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";

    infowindow = new google.maps.InfoWindow({
     content: html
    });

    google.maps.event.addListener(map, "click", function(event) {
        marker = new google.maps.Marker({
          position: event.latLng,
          map: map
        });
        google.maps.event.addListener(marker, "click", function() {
          infowindow.open(map, marker);
        });
    });

 center = bounds.getCenter();
 map.fitBounds(bounds); 

  }

  function saveData() {
      var name = escape(document.getElementById("name").value);
      var adress = escape(document.getElementById("adress").value);
      var city = escape(document.getElementById("city").value);
      var description = escape(document.getElementById("description").value);
      var type = document.getElementById("type").value;
      var latlng = marker.getPosition();

      var url = "phpsqlinfo_addrow.php?name=" + name + "&adress=" + adress + "&city=" + city +
      "&description=" + description + "&type=" + type + "&lat=" + latlng.lat() + "&lon=" + latlng.lng();
      downloadUrl(url, function(data, responseCode) {
        if (responseCode == 200 && data.length <= 1) {
          infowindow.close();
          document.getElementById("message").innerHTML = "Location added.";
        }

      });

     $(document).ready(function() {
        setTimeout("ReloadPage()", 100);
        });

    }

      function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request.responseText, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);

    }

    function ReloadPage() {
   location.reload();
};


    function doNothing() {}

PHP插入代码:

require("controller/phpsqlinfo_dbinfo.php");

// Gets data from URL parameters
$name = $_GET['name'];

$adress = $_GET['adress'];

$city = $_GET['city'];

$description = $_GET['description'];

$lat = $_GET['lat'];

$lon = $_GET['lon'];

$type = $_GET['type'];


// Opens a connection to a MySQL server
$connection=mysql_connect ("localhost", $dbuser, $dbpass);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($dbname, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}

// Insert new row with user data

//$submit="INSERT INTO poi_example (id, name, city, adress, description, lat, lon, type) VALUES (NULL,'$name','$city', '$adress','$description','$lat','$lon','$type');";

$query = sprintf("INSERT INTO poi_example " .
         " (id, name, adress, city, description, lat, lon, type ) " .
         " VALUES (NULL, '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
         mysql_real_escape_string($name),
         mysql_real_escape_string($adress),
          mysql_real_escape_string($city),
         mysql_real_escape_string($description),
         mysql_real_escape_string($lat),
         mysql_real_escape_string($lon),
         mysql_real_escape_string($type));

$result = mysql_query($query);


//print_r($result);

if (!$result) {
  die('Invalid query: ' . mysql_error());
}

?>

1 个答案:

答案 0 :(得分:1)

您的(本地?)数据库是否可能未配置为使用正确的编码?我认为UTF-8可能更适合立陶宛。我认为MySQL的标准编码就像latin1一样?这只是猜测,但您可能需要检查一下。