【问题标题】:Android Php Mysql insertion repetitionAndroid PHP Mysql插入重复
【发布时间】:2014-10-12 09:04:04
【问题描述】:

目前,我的 Android 应用程序只需获取用户的姓名和国家/地区,并在提交时将该数据插入到与 php 中的 httpclient 连接的 mysql 中,然后在下一个活动(Android)中,它会从我的数据库中读取所有数据并显示它。问题是每个插入在数据库中存储 2 个相同的值,而我的 php 脚本中只有一个查询。我完全不了解它背后的科学。期待支持。 Android 插入代码:

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();
?>

【问题讨论】:

    标签: php android mysql httpclient


    【解决方案1】:

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

    【讨论】:

    • 处理 HttpClient 的请求部分。 GET 和 POST 方法有什么关系吗?我在两边都使用 Post 但我认为在第二次活动中再次建立连接主要是导致问题
    • 显示所有你调用httpclient发送req的地方。问题可能出在那儿..尝试调试
    • 整理出来了。我在响应部分以某种方式重新发送了 httppost 请求。现在就像一个魅力。感谢您的帮助。我想我应该接受你的回答,因为如果不是确切的答案,它表明可能存在问题。似乎有道理。
    猜你喜欢
    • 2013-10-13
    • 1970-01-01
    • 2016-08-20
    • 2022-10-05
    • 1970-01-01
    • 2023-03-19
    • 2017-12-24
    • 2018-01-24
    • 1970-01-01
    相关资源
    最近更新 更多