When updating values into database, instead of updating 1 row each time(which is what we wanted), it update the whole database column. Here are my codes:
Java code:
private Dialog alertDialog() { final AlertDialog.Builder
alertDialog = new AlertDialog.Builder(NotificationActivity.this);
// Setting Dialog Title alertDialog.setTitle("Confirmation..."); // Setting Dialog Message alertDialog.setMessage("Do you want to accept this job?"); // Setting Icon to Dialog alertDialog.setIcon(R.drawable.ic_dialog_alert); // Setting Positive "Yes" Button alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { // Write your code here to execute after dialog Toast.makeText(getApplicationContext(), "Accept", Toast.LENGTH_SHORT).show(); // Return Result from alertdialog to database result = "accepted"; System.out.println("The result is "+ result); new UpdateActivity().execute(); } }); // Setting Negative "NO" Button alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Write your code here to execute after dialog Toast.makeText(getApplicationContext(), "Reject", Toast.LENGTH_SHORT).show(); dialog.cancel(); // Return Result from alertdialog to database result = "rejected"; System.out.println("The result is "+ result); new UpdateActivity().execute(); } }); return alertDialog.show(); }
/** * Background Async Task to update database * */ class UpdateActivity extends AsyncTask {
/** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(NotificationActivity.this); pDialog.setMessage("Sending ..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); }
/** * Saving product * */ protected String doInBackground(String... args) { System.out.println("Sending...");
// getting updated data from dialog String confirmation =
result.toString(); System.out.println("Result to string..." + confirmation );
// Building Parameters List<NameValuePair> params = new
ArrayList(); params.add(new BasicNameValuePair(TAG_ID, uid)); params.add(new BasicNameValuePair(TAG_RESULT, confirmation)); // getting JSON Object // Note that create product url accepts POST method JSONObject json = jsonParser.makeHttpRequest(UPDATE_URL, "POST", params); System.out.println("Json parsing..."); // check log cat fro response Log.d("Create Response", json.toString());
// check for success tag try { System.out.println("Checking..."); int success = json.getInt(TAG_SUCCESS); if (success == 1) { // successfully created product Intent i = new Intent(getApplicationContext(), JobsAcceptedActivity.class); startActivity(i); //Toast.makeText(getApplicationContext(), "Update successfully...", Toast.LENGTH_SHORT).show(); System.out.println("Successfully updated..."); // closing this screen finish(); } else { // failed to create product //Toast.makeText(getApplicationContext(), "Update unsucessfully...", Toast.LENGTH_SHORT).show(); System.out.println("Update unsuccessfully..."); } } catch (JSONException e) { e.printStackTrace(); } System.out.println("Done!"); return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(String file_url) { // dismiss the dialog once done pDialog.dismiss(); } } }
PHP code:
$response = array(); // check for required fields if (isset($_POST['UID']) && isset($_POST['accept'])) {
$UID = $_POST['UID']; $accept = $_POST['accept']; // include db connect class require_once __DIR__ . '/db_connect.php'; // connecting to db $db = new DB_CONNECT(); // mysql update row with matched pid $result = mysql_query("UPDATE notification SET accept = '$accept' WHERE UID = $UID"); // check if column inserted or not if ($result) { // successfully updated $response["success"] = 1; $response["message"] = "Notification successfully updated."; // echoing JSON response echo json_encode($response); } else { } } else { // required field is missing $response["success"] = 0; $response["message"] = "Required field(s) is missing"; // echoing JSON response echo json_encode($response);