在我的应用程序中,有一个注册屏幕,它发送用户的注册详细信息并将其发送到服务器,然后服务器将其存储在数据库中。
我的问题是,当我检查数据库时,有多个数据副本。我不知道多个副本是由客户端还是服务器端创建的。
我添加了数据库的屏幕截图,您可以清楚地看到有相同数据的多个副本。每当我在我的Android应用程序中按客户端的发送按钮时,两个相同的数据都存储在数据库中。我还没有使用任何循环,它仍然在数据库中存储两个相同数据的副本。
这是我的代码
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();
}
});
}
}
这是我的服务器端代码
<?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";
?>
这是数据库
的屏幕截图答案 0 :(得分:1)
好像你的调用在Login()
执行了两次?
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
答案 1 :(得分:0)
我不知道多个副本是否由客户端创建 或服务器端。
此代码每次运行时都会插入一个新行
$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());
您需要选择现有数据,然后才能插入行