Android使用PHP页面在数据库MySQL上输入重复数据

时间:2015-07-20 21:35:54

标签: java php android mysql database

我的Android应用程序出现问题。我正在尝试使用PHP页面插入数据库,该表具有auto_increment主键。它工作但是当我点击按钮时它保存了两次相同的数据。例如(1," Hello","`World")(2," Hello"," World")。如何纠正这个?

PHP页面:

if($tipo =="carro"){
    $cap=$_POST['capacidade'];
    $quilo=$_POST['quilometragem'];
    $tipo_vei=$_POST['tipo_veiculo'];
    $model=$_POST['modelo'];
    $plac=$_POST['placa'];
    $em=$_POST['email'];
    $bd = new Banco();
    $query_search  = "INSERT INTO veiculo VALUES(NULL,'".$cap."','".$quilo
        ."','".$tipo_vei."','".$em."','".$model."','".$plac."')";
    $query_exec =  mysqli_query($bd->con,$query_search);
    echo "inserido";
}

Android文件:

public class Carros2 extends MainActivity {
    TextView email;
    Button b;
    EditText capacidade,quilometragem,tipo,modelo,placa,tipo2;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs3;
    ProgressDialog dialog = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carros2);
        email = (TextView) findViewById(R.id.textView68);
        Intent it = getIntent();
        if (it != null) {
            Bundle params = it.getExtras();
            if (params != null) {
                String nome = params.getString("nome");
                email.setText(nome);
            }
        }
        capacidade = (EditText)findViewById(R.id.txtCapacidadeTanque);
        quilometragem = (EditText)findViewById(R.id.txtQuilometragem);
        tipo = (EditText)findViewById(R.id.txtTipo);
        tipo2 = (EditText)findViewById(R.id.tipo_carro);
        modelo = (EditText)findViewById(R.id.txtModelo);
        placa = (EditText)findViewById(R.id.txtPlaca);
        b=(Button)findViewById(R.id.insert_veiculo);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(Carros2.this, "", "Cadastrando...", true);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        login();
                    }
                }).start();
            }
        });
    }
    void login(){
        try{
            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://10.0.2.2:80/inseriruser_android.php");
            nameValuePairs3 = new ArrayList<NameValuePair>(7);
            nameValuePairs3.add(new BasicNameValuePair("capacidade",
                capacidade.getText().toString().trim()));
            nameValuePairs3.add(new BasicNameValuePair("quilometragem",
                quilometragem.getText().toString().trim()));
            nameValuePairs3.add(new BasicNameValuePair("tipo_veiculo",
                tipo.getText().toString().trim()));
            nameValuePairs3.add(new BasicNameValuePair("modelo",
                modelo.getText().toString().trim()));
            nameValuePairs3.add(new BasicNameValuePair("placa",
                placa.getText().toString().trim()));
            nameValuePairs3.add(new BasicNameValuePair("tipo",
                tipo2.getText().toString().trim()));
            nameValuePairs3.add(new BasicNameValuePair("email",
                email.getText().toString().trim()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs3));
            response=httpclient.execute(httppost);
            ResponseHandler<String> responseHandler;
            responseHandler = new BasicResponseHandler();
            final  String response = httpclient.execute(httppost,responseHandler);
            System.out.println("Response: "+ response);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv.setText("Resposta do Servidor:" + response);
                    dialog.dismiss();
                }
            });
            if(response.equalsIgnoreCase("inserido")) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(Carros2.this, "Inserido",
                            Toast.LENGTH_SHORT).show();
                        quilometragem.setText("");
                        capacidade.setText("");
                        modelo.setText("");
                        placa.setText("");
                        tipo.setText("");
                    }
                });
            }else {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(Carros2.this, "Não Inserido",
                            Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
        catch(Exception e){
            dialog.dismiss();
            System.out.println("Exception: " + e.getMessage());
        }
    }
}

0 个答案:

没有答案