dongtiao2105 2012-05-10 02:43
浏览 50
已采纳

Android到PHP错误 - 未识别的索引

when sending data to a php server through android I cam across this error: "Notice: Undefined index: IT in C:\xampp\htdocs\hello.php on line 4". After a few hours of messing around with it I can't solve my issue.

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

update method in 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());
    }}

I am working on the android emulator and do have the internet permission. As always help is greatly appreciated. EDIT 1: It appears the issue is in the android and not the php code.

  • 写回答

1条回答 默认 最新

  • dqe9657 2012-05-10 02:54
    关注

    You're not sending your query string propertly somehow, so there is no $_REQUEST['IT'] on the PHP side. do a var_dump($_REQUEST) to see what's coming through. Or better yet, don't use $_REQUEST. It's somewhat insecure. Better use the superglobal directly related to your HTTP method, _GET or _POST.

    As well, the Refresh header is non-standard. it'll work, but you shouldn't depend on it. An HTTP level redirect uses the Location: header. Refresh: is a non-standard old-school Netscape extension.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?