The data is not transmitted correctly to the php server

advertisements

I have an app that reads in Json data from phpmyadmin thru a php script and displayed in a list activity. Once a store name is clicked, +1 is added to the vote count for that store and is supposed to be sent back to the php server to store the new vote count in phpmyadmin. After the selection, I check the db vote count value and it is not updated. Although I get HTTP/1.1 200 ok in logcat, I don't think the data is being passed or taken in correctly. Can someone help, I'm stuck and have no direction.

Android code:

public void writeJSON() {
    String convertedID;
    String convertedVote;

    //convert int to string value to passed
    convertedID = new Integer(selectedID).toString();
    convertedVote = new Integer(selectedVote).toString();

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.2.2/kcstores.php");

    try {

       //writes the output to be stored in creolefashions.com/test2.php
       ArrayList <NameValuePair> nvps = new ArrayList <NameValuePair>(2);
            nvps.add(new BasicNameValuePair("storeUpdate", "update"));
        nvps.add(new BasicNameValuePair("storeID", convertedID));
        nvps.add(new BasicNameValuePair("storeVote", convertedVote));

        httppost.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        Log.i("writeJSON", response.getStatusLine().toString());

        } catch(Exception e) {
            Log.e("log_tag", "Error in http connection"+e.toString());
        }
}

PHP Code:

<?php
    $link = mysql_connect("localhost", "root", "") or die (mysql_error());
    mysql_select_db("king_cake_stores")or die (mysql_error());

    $query = "SELECT * FROM storeInfo";
    $result = mysql_query($query);
    $getUpdate = "noupdate";

    if (isset($_POST['storeUpdate'])) {
      echo "receiving data from app";
      $getUpdate = $_POST['storeUpdate'];
      $getStoreID = $_POST['storeID'];
      $getStoreVote = $_POST['storeVote'];
    }

    // If command == getStoreID, it updates the table storeVote value
    // with the android storeVote value based upon correct storeID
    if ($getUpdate == "update") {
       mysql_select_db("UPDATE storeInfo SET storeVote = $getStoreVote
          WHERE storeID == $getStoreID");
    } else {
    // stores the data in an array to be sent to android application
    while ($line = mysql_fetch_assoc($result)) $output[]=$line;
        print(json_encode($output));
    }
     mysql_close($link);

?>


I suggest you start debugging from the server end and start working your way backwards.

First, start logging the response text from your HttpResponse.

Echo the mysql query text in your php file, and make sure it looks the way you're expecting it to.

If it looks correct, check your database structure.

If not, try doing a var_dump($_POST), and check to see if your parameters are being sent correctly.

If you run through these steps, you should have a better idea of where the problem is.