将值更新到数据库时,不是每次更新 1 行(这是我们想要的),而是更新整个数据库列。这是我的代码:
Java代码:
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(); }
/** * 更新数据库的后台异步任务 * */ class UpdateActivity extends AsyncTask {
/** * 在启动后台线程之前显示进度对话框 * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(NotificationActivity.this); pDialog.setMessage("发送中..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); }
/** * 保存产品 * */ protected String doInBackground(String... args) { System.out.println("Sending...");
// getting updated data from dialog String confirmation =
结果.toString(); System.out.println("结果到字符串..." + 确认 );
// Building Parameters List<NameValuePair> params = new
数组列表(); params.add(new BasicNameValuePair(TAG_ID, uid)); params.add(new BasicNameValuePair(TAG_RESULT, 确认)); // 获取 JSON 对象 // 注意 create product url 接受 POST 方法 JSONObject json = jsonParser.makeHttpRequest(UPDATE_URL, "POST", params); System.out.println("Json 解析..."); // 检查日志 cat 来自响应 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代码:
$响应 = 数组();// 检查必填字段 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);