При обновлении значений в базе данных вместо обновления по 1 строке каждый раз (это то, что мы хотели), она обновляет весь столбец базы данных. Вот мои коды:Обновление базы данных mySQL
Java код:
private Dialog alertDialog() { final AlertDialog.Builder
alertDialog = новый 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(); }
/** * Фон Асинхронный Задача обновления базы данных */класс UpdateActivity расширяет AsyncTask {
/** * Перед началом фона темы Прогресс Диалог * */ @Override защищен недействительным onPreExecute() {super.onPreExecute(); pDialog = новый ProgressDialog (NotificationActivity.this); pDialog.setMessage («Отправка ...»); pDialog.setIndeterminate (false); pDialog.setCancelable (истина); pDialog.show(); }
/** * Сохранение продукта * */protected String doInBackground (String ... args) { System.out.println («Отправка ...»);
// getting updated data from dialog String confirmation =
результат.toString(); System.out.println («Результат для строки ...» + подтверждение);
// Building Parameters List<NameValuePair> params = new
ArrayList(); params.add (новый BasicNameValuePair (TAG_ID, uid)); params.add (новый BasicNameValuePair (TAG_RESULT, подтверждение)); // получение объекта JSON // Обратите внимание, что URL-адрес продукта принимает метод POST JSONObject json = jsonParser.makeHttpRequest (UPDATE_URL, "POST", params); System.out.println («Json parsing ...»); // check log cat fro response Log.d («Создать ответ», 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 код:
$ ответ = массив(); // проверка необходимых полей, если (Исеть ($ _ POST [ 'UID']) & & Исеть ($ _ POST [ 'принимает'])) {
$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);
У вас есть дублированные строки?. если нет, то проверьте, отправлен ли UID из кода Android. – k0sh