我正在尝试使用php将我的纬度和经度值发送到我的数据库。我有一个EditText,允许用户将他们的UserID发送到数据库工作正常,但当我尝试发送我的纬度和经度时,它无法将其添加到数据库。对于UserID,它是一个editText但我的lat和long是一个setText / textView。
将lat和long设置为textViews:
if (l != null) { //get latitude and longitude of the location
double lng = l.getLongitude();
double lat = l.getLatitude();
//display on text view
ln.setText("" + lng);
lt.setText("" + lat);
} else {
ln.setText("No Provider");
lt.setText("No Provider");
}
}
@Override
public void onLocationChanged(Location location) {
double lng = location.getLongitude();
double lat = location.getLatitude();
//display on text view
ln.setText("" + lng);
lt.setText("" + lat);
添加到数据库:
private void addEmployee(){
final String TextUserID = editTextUserID.getText().toString().trim();
final String lon = ln.getText().toString().trim();
final String lat = lt.getText().toString().trim();
class AddEmployee extends AsyncTask<Void,Void,String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MainActivity.this,"Adding...","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Void... v) {
HashMap<String,String> params = new HashMap<>();
params.put(Config.KEY_EMP_ID, TextUserID);
params.put(Config.KEY_LAT,lat);
params.put(Config.KEY_LON,lon);
Config.KEY_EMP_ID,Config.KEY_LAT,Config.KEY_LAT在我的Config.java中声明:
public static final String KEY_EMP_ID = "UserID";
public static final String KEY_LAT = "Latitude";
public static final String KEY_LON = "Longitude";
addEmp.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$UserID = $_POST['UserID'];
$Latitude = $_POST['Latitude'];
$Longitude = $_POST['Longitude'];
//Creating an sql query
$sql = "INSERT INTO locationtrace (UserID,Latitude,Longitude,) VALUES ('$UserID','$Latitude','$Longitude')";
//Importing our db connection script
require_once('dbConnect.php');
//Executing query to database
if(mysqli_query($con,$sql)){
echo 'Added Successfully';
}else{
echo 'Could Not Add To Database!';
}
//Closing the database
mysqli_close($con);
}