I need my Android app to update DB data. I am new to both Android dev and PHP, so this is kinda new to me, but I really need this done. Here is what I got:
Relevant piece of code (JSON object sent to the server):
HttpURLConnection conn = null;
OutputStream os = null;
InputStream is = null;
try {
URL url = new URL("my-server/update.php");
JSONObject jsonObject = new JSONObject();
jsonObject.put("id_person", ""+me.getId());
jsonObject.put("name", me.getName());
jsonObject.put("surname", me.getSurname());
jsonObject.put("lat", ""+me.getLatitude());
jsonObject.put("lng", ""+me.getLongitude());
jsonObject.put("phone", ""+123456789);
String msg = jsonObject.toString();
System.out.println(msg);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(20000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setFixedLengthStreamingMode(msg.getBytes().length);
conn.connect();
os = new BufferedOutputStream(conn.getOutputStream());
os.write(msg.getBytes());
os.flush();
is = conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
conn.disconnect();
}
The 'msg' string (JSON) I write is
"{"id_person":"1","name":"T","surname":"Test","lat":"12.34","lng":"43.21","phone":"123456"}
And the php I use:
<?php
$json = file_get_contents('php://input');
$obj = json_decode($json);
$id_person = $obj->{'id_person'};
$name = $obj->{'name'};
$surname = $obj->{'surname'};
$lat = $obj->{'lat'};
$lng = $obj->{'lng'};
$phone = $obj->{'phone'};
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("UPDATE person SET name = '$name', surname = '$surname', lat = '$lat', lng = '$lng', phone = '$phone' WHERE id_person = $id_person");
?>
What this does: No exceptions, no errors. App acts like its done, but no changes on DB happen. So my guess is I either send data the wrong way or work with them the wrong way in PHP file (or possibly both?).
Thanks to everyone who tries to help! ;)