2013-01-03 133 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