2013-01-21 42 views
1

我目前有一個與PHP和MySQL的Android應用程序,但我的問題是,我的代碼總是拋出0.000000經度和緯度,當它傳遞到PHP並將其存儲在MySQL中。我如何在MySQL數據庫中存儲GPS協調員?

,這是我的代碼

**

class myLocationlistener implements LocationListener { 
     @Override 
     public void onLocationChanged(Location location) { 
      if (location != null) { 
       double pLong = location.getLongitude(); 
       double pLat = location.getLatitude(); 
       textLat.setText(Double.toString(pLat)); 
       textLong.setText(Double.toString(pLong)); 
      } 
     } 
     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 
     } 
     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 
     } 
     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 
     } 
    } 

    class CreateNewProduct extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(Sample.this); 
      pDialog.setMessage("Sending Location"); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     /** 
     * Creating product 
     * */ 
     protected String doInBackground(String... args) { 
      String latitude = textLong.getText().toString(); 
      String longitude = textLat.getText().toString(); 

      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("latitude", latitude)); 
      params.add(new BasicNameValuePair("longitude", longitude)); 

      // getting JSON Object 
      // Note that create product url accepts POST method 
      JSONObject json = jsonParser.makeHttpRequest(url_create_product, 
        "POST", params); 

      // check log cat fro response 
      Log.d("Create Response", json.toString()); 

      // check for success tag 
      try { 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        // successfully created product 
        Intent i = new Intent(getApplicationContext(), Echos.class); 
        startActivity(i); 

        // closing this screen 
        finish(); 
       } else { 
        // failed to create product 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

**

,這是爲PHP

**

if ($_POST['latitude'] && $_POST['longitude'] != null) { 
    $db_connect = mysql_connect($db_host, $db_user, $db_password) or die(mysql_error()); 
    $db = mysql_select_db($db_name); 
    $latitude = $_POST['latitude']; 
    $longitude = $_POST['longitude']; 

    // mysql inserting a new row 
    $result = mysql_query("INSERT INTO gpslocation(latitude, longitude) VALUES('latitude', 'longitude')"); 


    // check if row inserted or not 
    if ($result) { 
     // successfully inserted into database 
     $response["success"] = 1; 
     $response["message"] = "Product successfully created."; 

     // echoing JSON response 
     echo json_encode($response); 
    } else { 
     // failed to insert row 
     $response["success"] = 0; 
     $response["message"] = "Oops! An error occurred."; 

     // echoing JSON response 
     echo json_encode($response); 
    } 
} else { 
    // required field is missing 
    $response["success"] = 0; 
    $response["message"] = "Required field(s) is missing"; 

    // echoing JSON response 
    echo json_encode($response); 
} 

**

如果有人對此有所瞭解,請善意回答,或者告訴我我是否誤導了我的問題。謝謝

+0

緯度和經度字段的類型是什麼? – IlyaDoroshin

+1

緯度是十進制(7,5),經度是十進制(8,5) –

+0

您將字符串插入到十進制字段中(您忘記了經度和緯度的$變量符號) – IlyaDoroshin

回答

2

這個問題似乎與您的PHP代碼。

$result = mysql_query("INSERT INTO gpslocation(latitude, longitude) VALUES('$latitude', '$longitude')"); 

將上面的行更改爲$符號並查看。

更好的是使用mysql_execute和預處理語句。請參閱this

+0

非常感謝你們,這是完善。 :) –

+0

歡迎。選擇答案作爲正確的答案,如果它的工作。 – shazin