【问题标题】:Sending geolocation through php to mysql通过php发送地理位置到mysql
【发布时间】:2013-10-01 12:54:26
【问题描述】:

我正在尝试通过 php 将地理定位数据从 android 设备发送到 mysql。由于某种原因,它不起作用。它不会出现在 mySQL 数据库中。我做错了吗?

AS3

var myLat;
var myLong;
if (Geolocation.isSupported){
    var my_geo:Geolocation = new Geolocation();
    my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
    my_geo.setRequestedUpdateInterval(100);
} 
function onGeoUpdate(event:GeolocationEvent):void {
    myLat = event.latitude;
    myLong = event.longitude;
    test1.text = myLat;
    test2.text = myLong;
}
// Send Geolocation
function sendGps():void {
    var variables:URLVariables = new URLVariables();
    var varSend:URLRequest = new URLRequest("http://www.mypage.com/handler.php");
    varSend.method = URLRequestMethod.POST;
    varSend.data = variables;
    var varLoader:URLLoader = new URLLoader;
    varLoader.dataFormat = URLLoaderDataFormat.TEXT;
    variables.myrequest = "write_gps";
    variables.username = userName;
    variables.lati = myLat;
    variables.longi = myLong;
    varLoader.load(varSend);    
}

var myTimer:Timer = new Timer(1000);
myTimer.addEventListener(TimerEvent.TIMER, runItAll);
myTimer.start();
function runItAll(event:TimerEvent):void {
     sendGps();
}

PHP

if (isset($_POST['myrequest']) && $_POST['myrequest'] == "write_gps") {
    $username = $_POST['username'];
    $latitude = $_POST['lati'];
    $longitude = $_POST['longi'];
    require_once "connect_to_mysql.php";
    $update_sql = mysql_query("UPDATE current SET longitude='$longitude',     latitude='$latitude' WHERE username='$username'");
    mysql_close();
};

【问题讨论】:

  • 如果它只是在数据库中有错误的数据类型怎么办? Tr 制作数据类型为VARCHAR 的经度和纬度列并再次检查。
  • 是的,一切就绪。甚至也尝试过 TEXT 。谢谢!

标签: php android mysql actionscript-3 geolocation


【解决方案1】:

您的代码对 SQL 注入开放,在与 MySQL 交互时您应该始终使用 PDO(http://php.net/manual/de/ref.pdo-mysql.php)。

在 PHP 代码中执行 print_r($_POST) 时是否获得了所需的数据?

【讨论】:

  • 感谢您的提醒!我试过了,似乎数据没有从 AS3 传递到 PHP。我想知道它是否与地理位置代码的结构有关......
【解决方案2】:

在 AS3 中,您的代码 varLoader 的一部分不受垃圾收集器的保护,当 URLLoader 不发送任何数据时,它可能会导致问题。为防止varLoader 被GC,您可以添加Event.COMPLETE 监听器而不使用弱引用:

varLoader.addEventListener(Event.COMPLETE, onLoaderComplete)

private function onLoaderComplete(event:Event):void
{
    URLLoader(event.target).removeEventListener(event.type, onLoaderComplete);
}

我还建议您使用URLLoaderDataFormat.VARIABLES 而不是URLLoaderDataFormat.TEXT,因为您发送的是 URL 编码参数。

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    相关资源
    最近更新 更多