目前,我的 Android 应用程序只需获取用户的姓名和国家/地区,然后在提交时将该数据插入到与 php 中的 httpclient 连接的 mysql 中,并在下一个 Activity (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();
?>
最佳答案
代码看起来没问题。应该只进行一次。我猜 httpclient 可能会请求两次。 1.在android中检查这一点。 2.另一个想法是,如果这不是问题,则使两列都是唯一的,这样就不会发生插入。
关于php - Android Php Mysql 插入重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26323491/