计算2个位置之间的距离并将结果插入新表

I have a form that required user to input their name, address, & education background to analyze it with fuzzy. when they press "Mark Position" button, it'll show on the screen their exact latitude & longitude. after that, I want to use the lat & long that they got to count their distance with some places that I already have in mysql (including the latitude & longitude of the places).

the "Mark Position" works fine, but the "Save" button seems to not calculate the distance & not save it to 2 new tables I already had (tj_masjid & tj_univ). here's my code :

<?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)");
    }

}

Firstly, all the php variables in the SQL query should be inside quotes. i.e. $tj_univ=mysql_query("INSERT INTO tj_univ(id_lokasi, id_univ, jarak) VALUES('$id_lokasi','$t_iduniv','$t_jarak')");

Secondly, you have not defined $id_lokasi anywhere in your code.