简体   繁体   English

使用php和MySQL保存数据

[英]Save data using php and MySQL

This is my android program. 这是我的android程序。 I want to save id=1 and name=0 into the database first. 我想先将id=1name=0保存到数据库中。 Then when I click a button again, it will become id=1 and name=1 . 然后,当我再次单击按钮时,它将变为id=1name=1 But my database still shows id=1 and name=0 even when I use count++. 但是,即使使用count ++,我的数据库仍显示id=1name=0 Why is name=0 and not name=1 ? 为什么name=0而不是name=1

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());
          }
         }
       });
  }
 }

You are increasing the count after getting the name. 获得名称后,您正在增加计数。 You need to do it before: 您需要先执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM