0

First of all my php script:

<?php

/*
 * Following code will update a product information
 * A product is identified by product id (pid)
 */

// array for JSON response
$response = array();


    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

// check for required fields
if (isset($_POST['uid']) && isset($_POST['name'])) {

    $uid = $_POST['uid'];
    $name = $_POST['name'];

    $look = mysql_query("SELECT name FROM users WHERE name ='$name'");
    if(mysql_num_rows($look) == 0){

    // mysql update row with matched pid
    $result = mysql_query("UPDATE users SET name = '$name' WHERE uid = $uid");



    // check if row inserted or not
    if ($result) {
        // successfully updated
        $response["success"] = 1;
        $response["message"] = "Product successfully updated.";

        // echoing JSON response
        echo json_encode($response);
    } else {

    }
} else {

    $response["success"] = 2;
    $response["message"] = "Email already exists! :-(";
}
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}

?>

So I check if the name already exists. If yes then is the "success" equals to 2 and if not then is it equals to 0 and the row should be updated. But I get an Error in Android. If the name don't already exists, it works fine, but if the value exists then the app crashed.. Here is the Async-Task class from Android:

/**
 * Background Async Task to  Save product Details
 */
class SaveProductDetails extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Toast.makeText(getApplicationContext(), "saving..", Toast.LENGTH_SHORT).show();
        // Displays the progress bar for the first time.
        mBuilder.setProgress(100, 0, true);
        mNotifyManager.notify(id, mBuilder.build());
    }

    /**
     * Saving product
     */
    protected String doInBackground(String... args) {

        // getting updated data from EditTexts
        String name = editTextUserName.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(StaticVariables.UID, idOfCustomer));
        params.add(new BasicNameValuePair(StaticVariables.NAME, name));

        // sending modified data through http request
        // Notice that update product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(StaticVariables.url_update_username,
                "POST", params);

        // check json success tag
        try {
            int success = json.getInt(StaticVariables.TAG_SUCCESS);

            if (success == 1) {
                // successfully updated
                Intent i = getIntent();
                // send result code 100 to notify about product update
                setResult(100, i);
                finish();
            } else if (success == 2) {
                // failed to update product
                Toast.makeText(getApplicationContext(), json.getString("message"), Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product uupdated
        // dismiss the dialog once product uupdated
        Toast.makeText(getApplicationContext(), "succesfully", Toast.LENGTH_SHORT).show();
        mBuilder.setContentText("Upload complete");
        // Removes the progress bar
        mBuilder.setProgress(0, 0, false);
        mNotifyManager.notify(id, mBuilder.build());
        finish();
    }
}
2
  • in the log is a nullpointer: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONObject.getInt(java.lang.String)' on a null object reference at com.example.fabi.cardview.Edit.EditUserNameActivity$SaveProductDetails.doInBackground(EditUserNameActivity.java:123) at com.example.fabi.cardview.Edit.EditUserNameActivity$SaveProductDetails.doInBackground(EditUserNameActivity.java:89) Commented Oct 4, 2015 at 19:17
  • edit the question instead of posting error messages in comments Commented Oct 4, 2015 at 19:19

1 Answer 1

1

You missing:

echo json_encode($response);

in this case:

else {

    $response["success"] = 2;
    $response["message"] = "Email already exists! :-(";
}
Sign up to request clarification or add additional context in comments.

Comments

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.