我需要帮助将数据从 android 发送到 php。我有这个android的java代码;
public void sendToDb()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("pLat",Double.toString(lat)));
nameValuePairs.add(new BasicNameValuePair("pLng",Double.toString(lng)));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://123456.com/welcome.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.i("postData", response.getStatusLine().toString());
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
我在我的 php 文件夹中有这个代码;
<?php
$pLat = $_POST['pLat'];
$pLng = $_POST['pLng'];
$result = mysql_query("update Users set lat='$pLat',lon='$pLng' where username='ozi');
if (mysql_affected_rows()==0)
{
$query = "INSERT INTO Users ";
$query .= "(lat,lon) VALUES ('$pLat','$pLng')";
}
}
?>
当我从手机发送数据时没有问题,但是在我查看 mysql lat 和 lot 行之后不是空的而是空的。您是否在代码中看到了问题?