[英]everytime i insert a row into a table with the help of http post from my android app, two rows get inserted
Android部分
final ProgressDialog p = new ProgressDialog(arg0.getContext()).show(arg0.getContext(),"Waiting for Server", "Accessing Server");
Thread thread = new Thread()
{
@Override
public void run() {
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://www.ng.in/m/z.php");
nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("z_101","3"));
nameValuePairs.add(new BasicNameValuePair("uname",info));
nameValuePairs.add(new BasicNameValuePair("bill",""+bill));
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() {
p.dismiss();
pay.append(response);
}
});
}catch(Exception e){
e.printStackTrace();
}
}
};
thread.start();
PHP代碼: -
<?php
require "connect.inc.php";
$uname=$_POST['uname'];
$bill=(int)$_POST['bill'];
$z_101=(int)$_POST['z_101'];
$rest="z";
$query1="insert into mc_log (uname,rname,bill) values ('$uname','$rest','$bill')";
$result1=mysql_query($query1);
?>
每次運行它時,兩行具有相同的值都會插入到mc_log表中。 mc_log表還有兩列i)id是主鍵,auto_increamented ii)時間,默認為systime。
此代碼看起來加倍:
//Execute HTTP Post Request
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
fnal String response = httpclient.execute(httppost, responseHandler);
你正在調用execute()
兩次,這就是問題......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.