1 Replies - 666 Views - Last Post: 13 May 2012 - 03:30 AM

#1 jersam515  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-May 12

Android to PHP error- Unidentified Index

Posted 12 May 2012 - 07:28 PM

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

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Android to PHP error- Unidentified Index

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,525
  • Joined: 23-August 08

Re: Android to PHP error- Unidentified Index

Posted 13 May 2012 - 03:30 AM

Can only speak to the PHP, but

$_REQUEST['IT']


Don't use $_REQUEST, it's insecure. You're POSTing to the server from the device, so use $_POST on the PHP server. Also never set a database value received from a user without first validating it and protecting against SQL injection. And while you're developing this, you should echo the query out on error as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1