我正在嘗試構建一個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));
}
_ 「注意:未定義的變量:目的地」 _嗯......你在哪裏定義它? –
[PHP:「注意:未定義的變量」,「注意:未定義的索引」和「注意:未定義的偏移量」)的可能的重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable- notice-undefined-index-and-notice-undef) – mkaatman
您的代碼易受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)。 –