I am trying to update a mysql database using php from an android application. I have successfully been able to update the database and return data from the database using this tutorial I found here: http://www.helloandroid.com/tutorials/connecting-mysql-database
I am now trying to update a database using a location provider to obtain the latitude and longitude of a location fix that I am getting via the onLocationChanged callback. My code looks like this:
public void onLocationChanged(Location location) {
double myLonDouble = location.getLongitude();
String myLon = Double.toString(myLonDouble);
double myLatDouble = location.getLatitude();
String myLat = Double.toString(myLatDouble);
sp = getSharedPreferences("MyPrefsFile", 0);
id = sp.getInt("id", 0);
String idString = Integer.toString(id);
DatabaseConnector db = new DatabaseConnector();
ArrayList<NameValuePair> latLon = new ArrayList<NameValuePair>();
latLon.add(new BasicNameValuePair("longitude", myLon));
latLon.add(new BasicNameValuePair("latitude", myLat));
latLon.add(new BasicNameValuePair("id", idString));
tv.setText(idString);
db.dbConnect(latLon, php);
}
The method dbConnect, which sends my data to a php file looks like this:
public void dbConnect(ArrayList<NameValuePair> data, String phpFile) {
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(phpFile);
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}// end dbConnect
The php file that I am using in the httppost object looks like this:
>><?php
>>$con = mysql_connect("localhost","********","*********");
>>if (!$con)
>> {
>> die('Could not connect: ' . mysql_error());
>> }
>>
>>mysql_select_db("swizzle_practice", $con);
>>
>>$_SQL =
>>"UPDATE users
>>SET longitude = '开发者_运维技巧$_REQUEST[longitude]', latitude = '$_REQUEST[latitude]'
>>WHERE id = '$_REQUEST[id]'";
>>
>>mysql_query($_SQL,$con) or die("error: " . mysql_error());
>>
>>mysql_close($con)
>>?>
My manifest looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.first_screen"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".First_screenActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Setup"
android:label="@string/app_name">
</activity>
<activity android:name=".Map"
android:label="@string/app_name">
</activity>
</application>
</manifest>
I am not getting any errors in the log which leads me to think that it is my code in the dbConnect method that is wrong. I have looked over it and can't seem to find what I am doing wrong.
Any help you can give would be much appreciated.
Louis
Add '$_REQUEST['longitude']'
, how is now? Log the nameValuePairs to check if the pairs values are correct.
$_SQL =
"UPDATE users
SET longitude = '$_REQUEST['longitude']', latitude = '$_REQUEST['latitude']'
WHERE id = '$_REQUEST['id']';";
P.S: I suggest test your php code and your query with manually inputed values.
And you don't need response
since you are doing operation which has nothing to do with your application after you send the key-values-pairs, i.e your query does not fetch any data. You can have it and you can use it for outputing if your query did the job or not.
You can check what data your script received using
foreach ($_POST as $var => $value) {
echo "$var = $value<br>n";
}
In your php script.
Well, I'm not very good with PHP, but from what I see..you're accessing request variable from _REQUEST
, but you're setting the request with HttpPost
.
So to put it straight, you're setting a POST request, but retrieving the variable on the server end as a GET request. To fix this, either make the client send a GET request or access request variable on the server side from _POST
精彩评论