0

I'm trying to get location(latitude, longitude) from android application and insert into database using php then extract data within a 10m radius.

The Problem is, when I test the code using smartphone (local test is OK), data is not correctly inserted. Table 'usergps' has 3 columns (name, latitude, longitude) and after I test the code, ( , 0, 0) is inserted.

Can you check the php code below? If it is correct, I think I should check the java code.

<?php
if(isset($_POST['name']) && $_POST['name'] != '') {
    require_once("include/db_info.php");
    $s=mysql_connect($SERV,$USER,$PASS) or die("fail to connect to mysql");
    mysql_select_db($DBNM);


    $lat = $_POST['lat'];
    $lon = $_POST['lon'];
    $name = $_POST['name'];

    $result= mysql_query("SELECT * FROM usergps WHERE name = '".$name."'");

    $row = mysql_num_rows($result);

    if($row == 0) {
        mysql_query("INSERT INTO usergps (name,latitude,longitude)
                    VALUES
                    ('".$name."', '".$lat."', '".$lon."')");

    }
    else {
        mysql_query("UPDATE usergps SET latitude = '".$lat."' WHERE name = '".$name."'");
        mysql_query("UPDATE usergps SET longitude = '".$lon."' WHERE name = '".$name."'");
    }


    $query = mysql_query("SELECT
                *,
        ( 6371 * acos( cos( radians('".$lat."') ) * cos( radians( latitude ) ) * cos( radians( longitude )
- radians('".$lon."') ) + sin( radians('".$lat."') ) * sin( radians( latitude ) ) ) ) AS distance
            FROM usergps
            HAVING distance <= 0.01
            ORDER BY distance ASC");

    if(!$query) die ('Unable to run query:'.mysql_error());

    $numpeople = mysql_num_rows($query);
    echo json_encode($numpeople);

} else {
    $response["error"] = TRUE;
    $response["error_msg"]= "Required parameter 'name' is missing!";
    echo json_encode($response);
}
?>
2
  • Show us your java code too. Commented Jun 5, 2015 at 5:48
  • Looks like you are receiving empty string in name. maybe it is issue with your form Commented Jun 5, 2015 at 5:50

1 Answer 1

0

Please try to echo the data you get from Android app: $lat, $lon to check whether it posts correct data or not.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.