2017-04-07 46 views
0

我正在嘗試構建一個Android應用程序,它查詢服務器以獲取給定目標的經度和緯度。但是,我的PHP代碼似乎有錯誤,因爲當我在Web瀏覽器中輸入地址時,它顯示以下錯誤。使用PHP獲取服務器的經度和緯度

Notice: Undefined variable: destination in C:\xampp\htdocs\serverfiles\btc.php on line 6 
{"result":[{"latitude":null,"longitude":null}]} 

這是我btc.php文件:

<?php 
if($_SERVER['REQUEST_METHOD']=='GET'){ 
$id = $_GET['destination']; 
$con = mysqli_connect("127.0.0.1", "root", "", "bustrack"); 

$sql = "SELECT * FROM updates WHERE destination='".$destination."'"; 
$r = mysqli_query($con,$sql); 
$res = mysqli_fetch_array($r); 
$result = array(); 
array_push($result,array(
"latitude"=>$res['latitude'], 
"longitude"=>$res['longitude'], 
) 
); 
echo json_encode(array("result"=>$result)); 
} 
+0

_ 「注意:未定義的變量:目的地」 _嗯......你在哪裏定義它? –

+1

[PHP:「注意:未定義的變量」,「注意:未定義的索引」和「注意:未定義的偏移量」)的可能的重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable- notice-undefined-index-and-notice-undef) – mkaatman

+0

您的代碼易受SQL注入攻擊。您應該使用[mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)或[PDO](http://php.net/manual/en/pdo.prepared- statement.php)按照[本文]中描述的準備語句(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)。 –

回答

1

$sql = "SELECT * FROM updates WHERE destination='".$destination."'"; 變量$destination不存在。你需要在使用它之前聲明它。我相信變量$id是你想要的,看你的代碼。

+0

它工作!謝謝!!!沒有注意到這樣一個愚蠢的錯誤。大聲笑!!! – John

0

這個問題是,你永遠不分配$destination一個變量:

$id = $_GET['destination']; 
$con = mysqli_connect("127.0.0.1", "root", "", "bustrack"); 

$sql = "SELECT * FROM updates WHERE destination='".$destination."'"; 

你應該這樣做:

$id = $_GET['destination']; 
$con = mysqli_connect("127.0.0.1", "root", "", "bustrack"); 

$sql = "SELECT * FROM updates WHERE destination='".$id."'";