2014-10-12 98 views
0

那么目前我的Android应用程序只需要用户的名称和国家,并提交插入数据到mysql连接与httpclient在PHP和下一个活动(Android)它读取所有数据从我的分贝并显示它。问题是,每个插入存储在数据库中2相同的值,而在我的PHP脚本中只有一个查询。我完全不了解它背后的科学。期待寻求支持。 Android的插入代码:Android Php Mysql插入重复

httpclient=new DefaultHttpClient(); 
httppost= new HttpPost("http://......./wp-mail.php"); 
//add your data 
nameValuePairs = new ArrayList<NameValuePair>(2); 
// 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("username",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value']; 
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim())); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

PHP代码插入:

<?php 
    //echo 'this'; 
    $hostname = "localhost"; 
    $database = "..."; 
    $username = "..."; 
    $password = "xyz"; 

    $cn = mysql_connect($hostname, $username, $password) or die(mysql_error()); 
    mysql_select_db($database); 

    $name=$_POST['username']; 
    $country=$_POST['password']; 

    $insert="INSERT INTO `downloader_details`(id,name,country) VALUES (NULL,'$name','$country')"; 

    $result1 = mysql_query($insert) or die(mysql_error()); 
    echo "Done Loading"; 
    mysql_close($cn); 
    exit(); 
?> 

PHP显示代码:

<?php 
    //echo 'this'; 
$hostname = "localhost"; 
$database = "..........."; 
$username = ".........."; 
$password = "........."; 

$cn = mysql_connect($hostname, $username, $password) or die(mysql_error()); 
mysql_select_db($database); 

$query="SELECT * FROM `downloader_details` ORDER BY id DESC LIMIT 8"; 
$result = mysql_query($query) or die(mysql_error()); 

echo "Following people around the world downloaded this application"; 

while($row = mysql_fetch_assoc($result)) 

{ 
    echo $row['name']. " From ". $row['country']."\n\n"; 
} 


mysql_close($cn); 
exit(); 
?> 

回答

0

代码看起来确定。应该只做一次。 httpclient可能会请求两次我猜.. 1.在android中检查此。 2.另一个想法是让两个列都是唯一的,如果这不是问题,那么插入不会发生。

+0

使用HttpClient的请求部分。 GET和POST方法有什么关系?我使用邮政在任何一方,但我认为在第二次活动中再次建立联系导致问题主要是 – 2014-10-12 09:53:05

+0

显示所有你称之为httpclient发送req。问题可能出现在那里..试着调试 – Panther 2014-10-12 13:52:22

+0

整理出来..我在response部分以某种方式重新发送了httppost请求。现在就像魅力一样。感谢您的帮助。我想我应该接受你的答案,因为如果不是确切的答案,它显示了可能出现的麻烦区域。看起来合理。 – 2014-10-12 19:37:09