1

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! ;)

1 Answer 1

1

You have currently got $person_id in the SQL query line and $id_person as the variable name, so the SQL string isn't complete.

As an aside - mysql_* is the older mysql extension in php and is deprecated, so if you are new to php it's probably worth looking at the new options aka mysqli or pdo_mysql ( http://php.net/manual/en/mysqlinfo.api.choosing.php )

As a second aside, you don't really want to be passing the parameters straight into the DB query like that as it opens door to SQL injection attacks. For the mysqli extension for example, you would normally make use of bound params - http://php.net/manual/en/mysqli-stmt.bind-param.php

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I guess that would never work then. However, I still experience the same problem, even after I fixed this one. So I guess there is something else.. And thanks for the tips, I will look it up, but for now I really need this to be working till tomorrow in any way...
if i set the json to your example, and run it locally it looked like query would be valid at that point - i suggest adding some echo statements (if your app willl show raw response form webserver if it errors) or use php's error_log function (php.net/manual/en/function.error-log.php) to get some visibility of whats going on.
So yea, the row did not update as I found by echoing the result. I managed to fix the "bug", and all it was was that the "p" in person table was not upper-case (I did not know it has to be). Changed that to Person and it works. Thanks for the first tip tho, I am accepting your answer, as I state the solution here in the comment. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.