我有一个表单要求用户输入他们的姓名,地址和&教育背景用模糊分析它。当他们按下“Mark Position”按钮时,它会在屏幕上显示他们的确切纬度和&经度。在那之后,我想使用lat&很久他们已经计算了我在mysql中已有的一些地方的距离(包括地方的纬度和经度)。
“标记位置”工作正常,但“保存”按钮似乎无法计算距离&不要把它保存到我已经拥有的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)");
}
}
答案 0 :(得分:0)
首先,SQL查询中的所有php变量都应该在引号内。即$tj_univ=mysql_query("INSERT INTO tj_univ(id_lokasi, id_univ, jarak) VALUES('$id_lokasi','$t_iduniv','$t_jarak')");
其次,您尚未在代码中的任何位置定义$ id_lokasi。