当通过android向php服务器发送数据时我发现了这个错误:“注意:未定义的索引:第4行的C:\ xampp \ htdocs \ hello.php中的IT”。经过几个小时的讨论后,我无法解决我的问题。
hello.php:
<?php
mysql_connect("localhost","user","pass");
mysql_select_db("mmo");
$r=mysql_query("update players set X = '".$_REQUEST['IT']."' where 22=22");
if(!$r)
echo "Error in query: ".mysql_error();
mysql_close();
header('Refresh: 0.01; URL=http://localhost/hello.php');
?>
android中的更新方法:
public void UpdateSeverWithPlayer()
{List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("IT","3"));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/hello.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}}
我正在使用Android模拟器,并且拥有互联网权限。一如既往的帮助非常感谢。 编辑1:问题出现在android而不是php代码中。
答案 0 :(得分:1)
你不是以某种方式发送你的查询字符串,所以PHP方面没有$_REQUEST['IT']
。做var_dump($_REQUEST)
看看会发生什么。或者更好的是,不要使用$ _REQUEST。这有点不安全。更好地使用与您的HTTP方法直接相关的超全局,_GET或_POST。
同样,Refresh
标头是非标准的。它会起作用,但你不应该依赖它。 HTTP级别重定向使用Location:标头。刷新:是一个非标准的老式Netscape扩展。