更新mySQL数据库

时间:2013-09-16 05:48:27

标签: java php json

将值更新到数据库时,不是每次更新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();      }
     

/ ** *后台异步任务更新数据库* * /类   UpdateActivity扩展了AsyncTask {

     

/ ** *开始后台线程之前显示进度对话框* * /     @Override protected void onPreExecute(){super.onPreExecute();         pDialog = new ProgressDialog(NotificationActivity.this);         pDialog.setMessage(“发送...”);         pDialog.setIndeterminate(假); pDialog.setCancelable(真);         pDialog.show(); }

     

/ ** *保存产品* * /受保护的字符串   doInBackground(String ... args){                 的System.out.println( “发送...”);

  // getting updated data from dialog         String confirmation =
     

result.toString();                 System.out.println(“结果为字符串...”+确认);

  // Building Parameters              List<NameValuePair> params = new
     

的ArrayList(); params.add(new BasicNameValuePair(TAG_ID,   UID)); params.add(new BasicNameValuePair(TAG_RESULT,confirmation));             //获取JSON对象                 //请注意,create product url接受POST方法                 JSONObject json = jsonParser.makeHttpRequest(UPDATE_URL,                         “POST”,params);                 System.out.println(“Json解析...”);                     //检查log 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代码:

  

$ response = array(); //检查必填字段   (isset($ _ POST ['UID'])&amp;&amp; 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);

2 个答案:

答案 0 :(得分:0)

问:将值更新到数据库时,不是每次更新1行(这是我们想要的),而是更新整个数据库列。

答:听起来你可能想要更新更严格的“where”条款:)

答案 1 :(得分:0)

这是你的==&gt; $ result = mysql_query(“UPDATE notification SET accept ='$ accept'WHERE UID = $ UID”);

我的Suggession ==&gt; $ result = mysql_query(“UPDATE notification SET accept ='$ accept'WHERE UID ='$ UID'”);或
$ queryString =“UPDATE notification SET accept =”。$ accept。“WHERE UID =”。$ UID;
$ result = mysql_query($ queryString);