2013-10-16 79 views
0

這是我的android程序。我想首先將id=1name=0保存到數據庫中。然後當我再次點擊一個按鈕時,它將變成id=1name=1。但即使我使用count ++,我的數據庫仍然顯示id=1name=0。爲什麼是name=0而不是name=1使用php和MySQL保存數據

import java.io.InputStream; 
import java.util.ArrayList; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class AndroidTestActivity extends Activity { 
JSONArray jArray; 
String result = null; 
InputStream is = null; 
StringBuilder sb=null; 
private int mCount= 0; 

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

    Button b1 = (Button) findViewById(R.id.button1); 

    b1.setOnClickListener(new Button.OnClickListener() { 
      @Override 

      public void onClick(View v) { 
      // TODO Auto-generated method stub 

      ArrayList nameValuePairs = new ArrayList(); 
      mCount++; 
      nameValuePairs.add(new BasicNameValuePair("id","1")); 
      nameValuePairs.add(new BasicNameValuePair("name",Integer.toString (mCount))); 

      //http post 
      try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httpost = new HttpPost("http://192.168.1.132/insert.php"); 
       httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httpost); 

      }catch(Exception e){ 
       Log.e("log_tag", "Error in http connection"+e.toString()); 
      } 
     } 
     }); 
    } 
} 

回答

0

您在獲取名稱後增加計數。你需要這樣做:

mCount++; 
nameValuePairs.add(new BasicNameValuePair("id","1")); 
nameValuePairs.add(new BasicNameValuePair("name",Integer.toString (mCount))); 
+0

我試着把它之前,之後和top.also不能... –