我正在开发一个示例安卓应用程序,其中有两个EditTexts和一个‘保存’按钮。它从EditTexts获取数据,并使用PHP将其发布到MySQL服务器。密码是错误的。
保存数据的onClick方法
public void saveMy(View v)
{
name=e1.getText().toString();
no=e2.getText().toString();
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://10.0.2.2/GujaratiApp/myPHP.php");
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("Name",name));
postParameters.add(new BasicNameValuePair("No",no));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpclient.execute(httppost);
Log.i("postData", response.getStatusLine().toString());
}
catch(Exception ex)
{
Log.e("log_tag", "Error: "+ex.toString());
}
}PHP代码是:
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("newDB");
$name = $_POST['Name'];
$no = $_POST['No'] ;
$query_add="INSERT INTO myData (`name` ,`no` ) VALUES ('.$name','.$no')";
$query_exec=mysql_query($query_add) or die(mysql_error());
mysql_close();?>
当我单击“保存”按钮时,在LogCat中出现一条消息,提示HTTP/1.1200OK。当我在MySQL监视器中启动查询时,它会向表中添加一行,但是添加的行不会显示任何内容。如下图所示:

我不知道哪里出了问题..请帮帮我。
提前谢谢。
发布于 2012-08-19 13:59:29
请对mySQL中的列添加null检查,使这些字段成为必填字段。
ALTER TABLE myData Modify name varchar(xyz) NOT NULL;
ALTER TABLE myData Modify no varchar(abc) NOT NULL;在此之后,请检查您是否获得了响应状态200。这将验证post参数是否在服务上正确接收。如果不是,那么您可以尝试通过url本身传递这些参数,方法是将其附加到URL。
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://10.0.2.2/GujaratiApp/myPHP.php?name=" + name + "&no=" + no);
HttpResponse response = httpclient.execute(httppost);这应该会让你继续下去,如果
UrlEncodedFormEntity(postParameters)不起作用。
https://stackoverflow.com/questions/12024497
复制相似问题