0

我有一个表格,要求用户输入他们的姓名、地址和教育背景来模糊分析。当他们按下“标记位置”按钮时,它会在屏幕上显示他们的确切纬度和经度。在那之后,我想使用他们必须计算他们与我在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)");
    }

}
4

1 回答 1

0

首先,SQL 查询中的所有 php 变量都应该在引号内。IE$tj_univ=mysql_query("INSERT INTO tj_univ(id_lokasi, id_univ, jarak) VALUES('$id_lokasi','$t_iduniv','$t_jarak')");

其次,您没有在代码中的任何地方定义 $id_lokasi。

于 2013-05-29T18:45:34.270 回答