2013-01-03 83 views
0
存储到MySQL数据库从Android应用程序生成的纬度和经度值

我曾在android系统的跟踪应用程序,它成功地生成纬度和经度需要使用PHP

但我需要保存在我的外部MySQL的纬度和长使用PHP的数据库。

我使用的数值类型为“十进制(10,7)”,经纬度在数据库中。

任何想法如何使用PHP将Android中产生的经度和长度存储到我的外部mySQL数据库中?

我的PHP代码

<?php 
     $lat = $_GET['lat']; 
     $lng = $_GET['lng']; 
     $sender=$_POST['sender']; 

$hostname = 'localhost'; 
$username = 'xxxx'; 
$password = 'xxxxxxx'; 

    $conn = mysql_connect($hostname,$username,$password) or die('Error Please Try Again.'); 

if ($conn) { 

    mysql_select_db("android track", $conn); 


    $query2 = "INSERT INTO coor VALUES(NULL,'$sender','$lat','$lng')"; 

    if (mysql_query($query2, $conn)) 
     echo "Data Insertion Successful"; 
    else 
     echo "Oops". mysql_error() ; 

    } 

mysql_close(); 
?> 
+1

你需要什么样的信息?如何将android中的值传递给PHP或如何将它们存储在数据库中? – flo

+0

我需要将纬度和长度值从android存储到mySQL数据库 –

回答

1

您可以使用下面的代码提交mLatmLong变量到服务器:

String storeURL = "http://www.yourdomain.com/yourscript.php?lat=" + mLat + "&long=" + mLong; 

URL getURL; 
getURL = new URL(storeURL); 

URLConnection connection = getURL.openConnection(); 
connection.connect(); 

InputStream input = new BufferedInputStream(getURL.openStream()); 
OutputStream output = new ByteArrayOutputStream();   
byte data[] = new byte[1024]; 

while ((count = input.read(data)) != -1) { 
output.write(data, 0, count); 
} 

output.flush(); 
output.close(); 
input.close(); 

String downloadResult = output.toString(); 
Log.i("LOG", downloadResult); //Your php-script can, for example, "echo("OK");" if the storing was successful 

你仍然需要照顾的错误处理(没有网络连接等),当然。前

+0

感谢您的快速响应。我尝试了你的代码,但仍然无法获取经纬度并保存在数据库中。 –

+1

你有代码的php方面工作吗?即你有一个PHP脚本,它从'GET'变量('?lat =&long =')中获取值并将其存储在数据库中? – Nick

+0

等待我附上我使用的php代码 –

1

好吧,几件事我重写代码...

  1. 您正在打开自己注册到SQL注入攻击,因为你 不使用准备好的语句。
  2. 不推荐使用mysql PHP接口,而应使用mysqli或PDO代替 。
  3. 我假设你传递NULL坐标表上的主键。不要 这样做,而是将其标记为auto increment,并在 SQL中明确指定您的列。
  4. 什么是坐标表的create table语句?将SHOW CREATE TABLE coordinates的结果粘贴到您的问题中。我怀疑你的值为null 的列为primary key,并且永远不能为空。
+0

仍然没有进展,我仍然无法存储经纬度和长值db –

+0

我仍然没有看到' SHOW CREATE TABLE坐标@ @CoolJCoolie – hd1