2013-03-21 13 views
-4

此代碼它總是給我: {「消息」:「必填字段(一個或多個)丟失」,「成功」:0}總是在else體內的程序。爲什麼?

,這意味着它沒有進入,如果身體!它總是去別的!我不知道爲什麼!

reg_Doc_to_Patient.php 
========== 

    <?php 

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

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

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


     $id = $_POST['id']; 
     $Doc_id = $_POST['Doc_id']; 

     // 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 patients SET Doc_id = '$Doc_id' WHERE id = $id"); 

     // 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 { 
     // required field is missing 
     $response["success"] = 0; 
     $response["message"] = "Required field(s) is missing"; 

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


AllRequests.java 
========== 

    package com.example.rpm; 

    import java.util.ArrayList; 
    import java.util.HashMap; 
    import java.util.List; 

    import org.apache.http.NameValuePair; 
    import org.apache.http.message.BasicNameValuePair; 
    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 

    import android.app.ListActivity; 
    import android.app.ProgressDialog; 
    import android.content.Intent; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.View; 
    import android.widget.AdapterView; 
    import android.widget.ListAdapter; 
    import android.widget.ListView; 
    import android.widget.SimpleAdapter; 
    import android.widget.TextView; 
    import android.widget.AdapterView.OnItemClickListener; 
    import android.widget.Toast; 

    import com.example.rpm.Accept.SaveProductDetails; 
    import com.example.rpm.AllDoctor.LoadAllProducts; 

    public class AllRequests extends ListActivity { 

     // Progress Dialog 
      private ProgressDialog pDialog; 

      // Creating JSON Parser object 
      JSONParser jParser = new JSONParser(); 

      JSONParser jsonParser = new JSONParser(); 


      ArrayList<HashMap<String, String>> productsList; 

      // url to get all products list 
      private static String url_all_products = "http://10.0.2.2/RPM-connect/get_all_requests.php"; 
      private static String url_edit_products = "http://10.0.2.2/RPM-connect/reg_Doc_to_Patient.php"; 



      // JSON Node names 
      private static final String TAG_SUCCESS = "success"; 
      private static final String TAG_DOCTOR = "products"; 

      private static final String TAG_Patient_ID = "Patient_ID"; 
      private static final String TAG_Doctor_ID= "Doctor_ID"; 

      private static String id=""; // of patient 
      private static String Doc_id="";// of Doctor 

      // products JSONArray 
      JSONArray doctor = null; 

      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.requests); 

       // Hashmap for ListView 
       productsList = new ArrayList<HashMap<String, String>>(); 

       // Loading products in Background Thread 
       new LoadAllProducts().execute(); 

       // Get listview 
       ListView lv = getListView(); 

       // on seleting single product 
       // launching Edit Product Screen 
       lv.setOnItemClickListener(new OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> parent, View view, 
          int position, long idd) { 
         // getting values from selected ListItem 


         final String p_Id = ((TextView) view.findViewById(R.id.p_id)).getText().toString(); 
         final String d_Id = ((TextView) view.findViewById(R.id.d_id)).getText().toString(); 


         id= p_Id; 
         Doc_id= d_Id; 

         new VerivyDocToPatient().execute(); 

         Toast.makeText(getApplicationContext(), "yes", 500).show(); 
         // Starting new intent 
         //Intent in = new Intent(getApplicationContext(), AllRequests.class); 
         // sending pid to next activity 
         //in.putExtra(TAG_ID, Id); 


         // starting new activity and expecting some response back 
        // startActivityForResult(in, 100); 
        } 
       }); 

      } 

      // Response from Edit Product Activity 
      @Override 
      protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
       super.onActivityResult(requestCode, resultCode, data); 
       // if result code 100 
       if (resultCode == 100) { 
        // if result code 100 is received 
        // means user edited/deleted product 
        // reload this screen again 
        Intent intent = getIntent(); 
        finish(); 
        startActivity(intent); 
       } 

      } 

      /** 
      * Background Async Task to Load all product by making HTTP Request 
      * */ 
      class LoadAllProducts extends AsyncTask<String, String, String> { 

       /** 
       * Before starting background thread Show Progress Dialog 
       * */ 
       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        pDialog = new ProgressDialog(AllRequests.this); 
        pDialog.setMessage("Loading doctors. Please wait..."); 
        pDialog.setIndeterminate(false); 
        pDialog.setCancelable(false); 
        pDialog.show(); 
       } 

       /** 
       * getting All products from url 
       * */ 
       protected String doInBackground(String... args) { 
        // Building Parameters 
        List<NameValuePair> params = new ArrayList<NameValuePair>(); 
        // getting JSON string from URL 
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params); 

        // Check your log cat for JSON reponse 
        Log.d("All Products: ", json.toString()); 

        try { 
         // Checking for SUCCESS TAG 
         int success = json.getInt(TAG_SUCCESS); 

         if (success == 1) { 
          // products found 
          // Getting Array of Products 
          doctor = json.getJSONArray(TAG_DOCTOR); 

          // looping through All Products 
          for (int i = 0; i < doctor.length(); i++) { 
           JSONObject c = doctor.getJSONObject(i); 


           // Storing each json item in variable 

           String Patient_ID = c.getString(TAG_Patient_ID); 
           String Doctor_ID = c.getString(TAG_Doctor_ID); 



           // creating new HashMap 
           HashMap<String, String> map = new HashMap<String, String>(); 

           // adding each child node to HashMap key => value 

           map.put(TAG_Patient_ID, Patient_ID); 
           map.put(TAG_Doctor_ID, Doctor_ID); 



           // adding HashList to ArrayList 
           productsList.add(map); 
          } 
         } else { 
          // no products found 
          // Launch Add New product Activity 
          Intent i = new Intent(getApplicationContext(), 
            Adddoctor.class); 
          // Closing all previous activities 
          i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(i); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

        return null; 
       } 

       /** 
       * After completing background task Dismiss the progress dialog 
       * **/ 
       protected void onPostExecute(String file_url) { 
        // dismiss the dialog after getting all products 
        pDialog.dismiss(); 
        // updating UI from Background Thread 
        runOnUiThread(new Runnable() { 
         public void run() { 
          /** 
          * Updating parsed JSON data into ListView 
          * */ 
          ListAdapter adapter = new SimpleAdapter(
            AllRequests.this, productsList, 
            R.layout.list_requests, new String[] { TAG_Doctor_ID,TAG_Patient_ID }, 
            new int[] { R.id.p_id, R.id.d_id 
              }); 
          // updating listview 
          setListAdapter(adapter); 


         } 
        }); 

       } 

      } 

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

       /** 
       * Before starting background thread Show Progress Dialog 
       * */ 
       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        pDialog = new ProgressDialog(AllRequests.this); 
        pDialog.setMessage("verifying..."); 
        pDialog.setIndeterminate(false); 
        pDialog.setCancelable(true); 
        pDialog.show(); 
       } 

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

        // getting updated data from EditTexts 
        //String ID = txtID.getText().toString(); 
        /*String Fname = txtFname.getText().toString(); 
        String Sname = txtSname.getText().toString(); 
        String Lname = txtLname.getText().toString(); 
        String Specialist = txtSpecialist.getText().toString(); 
        String Gender = txtGender.getText().toString(); 
        String Workphone = txtWorkphone.getText().toString(); 
        String password = txtpassword.getText().toString(); 
        */ 

        // Building Parameters 
        List<NameValuePair> params = new ArrayList<NameValuePair>(); 

        params.add(new BasicNameValuePair(TAG_Patient_ID, id)); 
        params.add(new BasicNameValuePair(TAG_Doctor_ID, Doc_id)); 

        // sending modified data through http request 
        // Notice that update product url accepts POST method 
        JSONObject json = jsonParser.makeHttpRequest(url_edit_products, 
          "POST", params); 
        Log.d("aaaa",json.toString()); 

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

         if (success == 1) { 
          // successfully updated 
          Intent i = getIntent(); 
          // send result code 100 to notify about product update 
          setResult(100, i); 
          finish(); 
         } else { 
          // failed to update product 
         } 
        } 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 
        pDialog.dismiss(); 
       } 
      } 

    } 

有幫助嗎?

+1

也許有一個必填字段丟失。 – 2013-03-21 23:29:39

+0

暫時刪除'isset'。這將提供一些有關不匹配的請求方法和數組訪問的提示。 – mario 2013-03-21 23:34:43

+0

在開始時拋出'print_r($ _ POST)'來轉儲'$ _POST'的內容。你會看到POST請求變量。 – 2013-03-21 23:43:57

回答

3

在您的Java代碼中,您使用Patient_IDDoctor_ID作爲參數名稱,但PHP正在檢查idDoc_id

0

我不確定這是否是實際問題。

isset($_POST['id']) && isset($_POST['Doc_id']) 

params.add(new BasicNameValuePair(TAG_Patient_ID, id)) 

params.add(new BasicNameValuePair(TAG_Doctor_ID, Doc_id)) 

看看你是否在這裏檢查正確的名稱。

相關問題