Php Android JSON解析器将数据的多个副本发送到服务器

Php Android JSON解析器将数据的多个副本发送到服务器,php,android,mysql,json,Php,Android,Mysql,Json,在我的应用程序中,有一个注册屏幕,它向用户发送注册详细信息并将其发送到服务器,然后服务器将其存储在数据库中 我的问题是,当我检查数据库时,有多个数据副本。我不知道是客户端还是服务器端创建了多个副本 我已经添加了数据库的屏幕截图,您可以清楚地看到相同数据的多个副本。在我的android应用程序中,每当我按下客户端的send按钮,数据库中就会存储两个相同的数据。我没有使用任何循环,但它仍在数据库中存储相同数据的两个副本 这是我的密码 public class RegisterScreen exten

在我的应用程序中,有一个注册屏幕,它向用户发送注册详细信息并将其发送到服务器,然后服务器将其存储在数据库中

我的问题是,当我检查数据库时,有多个数据副本。我不知道是客户端还是服务器端创建了多个副本

我已经添加了数据库的屏幕截图,您可以清楚地看到相同数据的多个副本。在我的android应用程序中,每当我按下客户端的send按钮,数据库中就会存储两个相同的数据。我没有使用任何循环,但它仍在数据库中存储相同数据的两个副本

这是我的密码

public class RegisterScreen extends Activity {

    Button reg;
    TextView tv;
    EditText fn,ln,un,pwd,mob,email;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs;
    ProgressDialog dialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        reg=(Button)findViewById(R.id.reg);
        fn=(EditText)findViewById(R.id.fn);
        ln=(EditText)findViewById(R.id.ln);
        un=(EditText)findViewById(R.id.un);
        pwd=(EditText)findViewById(R.id.pwd);
        mob=(EditText)findViewById(R.id.mob);
        email=(EditText)findViewById(R.id.email);
        tv = (TextView)findViewById(R.id.tv);


        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(RegisterScreen.this, "",
                        "Registering user...", true);
                new Thread(new Runnable() {
                    public void run() {
                        login();
                    }
                }).start();
            }
        });
    }


    void login(){
        try{

            httpclient=new DefaultHttpClient();
            httppost= new HttpPost("http://192.168.43.6/test/registration.php"); // make sure the url is correct.
            //add your data
            nameValuePairs = new ArrayList<NameValuePair>();
            // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
            nameValuePairs.add(new BasicNameValuePair("firstname",fn.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("lastname",ln.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("username",un.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("password",pwd.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("mob",mob.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("email",email.getText().toString().trim()));// $Edittext_value = $_POST['Edittext_value'];

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //Execute HTTP Post Request
            response=httpclient.execute(httppost);

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);
            System.out.println("Response : " + response);
            runOnUiThread(new Runnable() {
                public void run() {
                    tv.setText("Response from PHP : " + response);
                    dialog.dismiss();
                }
            });

            if(response.equalsIgnoreCase("Registration")){
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(RegisterScreen.this,"Registration Successful", Toast.LENGTH_SHORT).show();
                    }
                });

                startActivity(new Intent(RegisterScreen.this, LoginScreen.class));
            }else{
                showAlert();
            }

        }catch(Exception e){
            dialog.dismiss();
            System.out.println("Exception : " + e.getMessage());
        }
    }
    public void showAlert(){
        RegisterScreen.this.runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterScreen.this);
                builder.setTitle("Registration Error.");
                builder.setMessage("Problem in Registration. Please try again later")
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }
        });
    }
}
公共类注册表屏幕扩展活动{
按钮注册;
文本视图电视;
编辑文本fn、ln、un、pwd、mob、电子邮件;
HttpPost-HttpPost;
字符串缓冲区;
HttpResponse响应;
HttpClient-HttpClient;
列出nameValuePairs;
ProgressDialog=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u寄存器);
reg=(按钮)findViewById(R.id.reg);
fn=(EditText)findViewById(R.id.fn);
ln=(EditText)findViewById(R.id.ln);
un=(EditText)findViewById(R.id.un);
pwd=(EditText)findViewById(R.id.pwd);
mob=(EditText)findViewById(R.id.mob);
email=(EditText)findviewbyd(R.id.email);
tv=(TextView)findviewbyd(R.id.tv);
reg.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
dialog=ProgressDialog.show(RegisterScreen.this,“,
“正在注册用户…”,正确);
新线程(newrunnable()){
公开募捐{
登录();
}
}).start();
}
});
}
无效登录(){
试一试{
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://192.168.43.6/test/registration.php“”;//请确保url正确无误。
//添加您的数据
nameValuePairs=新的ArrayList();
//发布时始终使用相同的变量名,即android端变量名和php端变量名应相似,
添加(新的BasicNameValuePair(“firstname”,fn.getText().toString().trim());
添加(新的BasicNameValuePair(“lastname”,ln.getText().toString().trim());
添加(新的BasicNameValuePair(“用户名”,un.getText().toString().trim());
添加(新的BasicNameValuePair(“密码”,pwd.getText().toString().trim());
添加(新的BasicNameValuePair(“mob”,mob.getText().toString().trim());
nameValuePairs.add(新的BasicNameValuePairs(“email”,email.getText().toString().trim());//$Edittext\u value=$\u POST['Edittext\u value'];
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
response=httpclient.execute(httppost);
ResponseHandler ResponseHandler=新BasicResponseHandler();
最终字符串响应=httpclient.execute(httppost,responseHandler);
System.out.println(“响应:+Response”);
runOnUiThread(新的Runnable(){
公开募捐{
setText(“来自PHP的响应:+Response”);
dialog.dismise();
}
});
if(响应.相等信号情况(“注册”)){
runOnUiThread(新的Runnable(){
公开募捐{
Toast.makeText(RegisterScreen.this,“注册成功”,Toast.LENGTH_SHORT.show();
}
});
startActivity(新意图(RegisterScreen.this、LoginScreen.class));
}否则{
showarert();
}
}捕获(例外e){
dialog.dismise();
System.out.println(“异常:+e.getMessage());
}
}
公开作废showAlert(){
RegisterScreen.this.runOnUiThread(新的Runnable(){
公开募捐{
AlertDialog.Builder=新建AlertDialog.Builder(RegisterScreen.this);
builder.setTitle(“注册错误”);
builder.setMessage(“注册有问题,请稍后再试”)
.setCancelable(错误)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
}
});
AlertDialog alert=builder.create();
alert.show();
}
});
}
}
这是我的服务器端代码

<?php
$hostname_localhost ="localhost";
$database_localhost ="testdb";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$password = $_POST['password'];
$mob = $_POST['mob'];
$email = $_POST['email'];

$query_search = "INSERT INTO register(Firstname, Lastname, loginname, pwd, Mobilenumber , emailid) VALUES('$firstname', '$lastname','$username','$password','$mob','$email')";
$query_exec = mysql_query($query_search) or die(mysql_error());
//$rows = mysql_num_rows($query_exec);
//echo $rows;
 echo "Registration";
?>

我不知道多个拷贝是否由客户端创建
或者服务器端

此代码在每次运行时插入新行

$query_search = "INSERT INTO register(Firstname, Lastname, loginname, pwd, Mobilenumber , emailid) VALUES('$firstname', '$lastname','$username','$password','$mob','$email')";
$query_exec = mysql_query($query_search) or die(mysql_error());

您需要为现有数据选择,然后才插入一行

似乎您的调用在
Login()
中执行了两次

response=httpclient.execute(httppost);
ResponseHandler ResponseHandler=新BasicResponseHandler();
最终字符串响应=httpclient.execute(httppost,responseHandler);

对不起,我没有得到你的答案。你能更深入地解释一下吗?首先,你需要选择一个必须是唯一的字段,例如电子邮件。然后运行查询“从
注册表中选择1,其中
emailid
=“$email”。如果该行不存在,请运行INSERT queryi将数据从android应用程序发送到php服务器,以便它可以将数据存储在数据库中,而不是检索数据。它只需要将其存储在数据库中,什么都不需要
response=httpclient.execute(httppost);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);