我有一個表單,要求用戶輸入他們的姓名,地址,&教育背景以模糊分析。當他們按下「標記位置」按鈕時,它將在屏幕上顯示他們的經度爲&的經度。在那之後,我想使用經緯度來計算他們與我已經在mysql中擁有的某些地方的距離(包括地點的經度緯度&)。計算2個位置之間的距離並將結果插入新表格
「標記位置」工作正常,但「保存」按鈕似乎不計算距離&不保存到我已有的2個新表(tj_masjid & tj_univ)。這裏是我的代碼:
<?php
include 'connect.php';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
$db = mysql_select_db($dbname);
if($_POST["button"]=="Save")
{
$nama=$_POST["nama"]; //name
$alamat =$_POST["alamat"]; //address
$pendidikan =$_POST["pendidikan"]; //educational background (years)
$latitude=$_POST["latitude"];
$longitude=$_POST["longitude"];
$query="INSERT INTO lokasi(nama, alamat, pendidikan, latitude, longitude) VALUES('$nama', '$alamat', '$pendidikan', '$latitude', '$longitude')"; //insert to table location
$result=mysql_query($query);
if($result){
echo "data berhasil disimpan"; //saving success
$id_lokasi=mysql_query('SELECT id from lokasi where nama=nama, alamat=alamat, pendidikan=pendidikan, latitude=latitude, longitude=longitude');
}else{
echo "data gagal disimpan"; //saving failed
}
//calculate distance in kilometers
function getDistanceBetween($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi')
{
$theta = $longitude1 - $longitude2;
$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
switch($unit)
{
case 'Mi': break;
case 'Km' : $distance = $distance * 1.609344;
}
return (round($distance,2));
}
//calculate the distance between users location & masjid location (2 masjid)
$sql_masjid=mysql_query("SELECT * from masjid");
while($rs = mysql_fetch_array($sql_masjid, MYSQL_ASSOC)) {
$masjid[$rs['id']] = $rs;
$j_masjid[$rs['id']] = getDistanceBetween($latitude,$longitude,$rs['latitude'],$rs['longitude'],'Km');
$t_jarak = $j_masjid[$rs['id']];
$t_idmasjid = $rs['id'];
$tj_masjid=mysql_query("INSERT INTO tj_masjid(id_lokasi, id_masjid, jarak) VALUES($id_lokasi,$t_idmasjid,$t_jarak)");
}
//calculate the distance between users location & university location (2 univ)
$univ=mysql_query("SELECT * from universitas");
while($rs = mysql_fetch_array($univ, MYSQL_ASSOC)) {
$univ[$rs['id']] = $rs;
$j_univ[$rs['id']] = getDistanceBetween($latitude,$longitude,$rs['latitude'],$rs['longitude'],'Km');
$t_jarak = $j_univ[$rs['id']];
$t_iduniv = $rs['id'];
$tj_univ=mysql_query("INSERT INTO tj_univ(id_lokasi, id_univ, jarak) VALUES($id_lokasi,$t_iduniv,$t_jarak)");
}
}
是不是已經在內部報價了? –
我在這裏嘗試你的建議:「$ query = mysql_query(」INSERT INTO lokasi(nama,alamat,pendidikan,latitude,longitude)VALUES('$ nama','$ alamat','$ pendidikan','$ latitude', '$東經') 「);」 。並且仍然不起作用 –