1

my code on php:

<?php
$username ='root';
$password ='abc';
$hostname ='localhost';
$database ='test_xmpp'; 
$localhost = mysql_connect($hostname,$username,$password) or 
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database,$localhost);
$groupmates = array();
$abc=mysql_query('SELECT count(1) FROM groupchat');
$groupcount= mysql_result($abc,0);
$groupcount = $groupcount + 1;
echo $groupcount;
$admin = $_POST["admin"];
$groupname = $_POST["groupname"];
$groupmates = $_POST["groupmates"];
$sql="INSERT INTO groupchat (idgroupchat,groupname) VALUES ('$groupcount','$groupname')";
mysql_query($sql);
$b = '2';
$a = $groupmates[0];
$sql1="INSERT INTO groupuser (idgroup, username, admin) VALUES ('$groupcount','$a','$b')";
mysql_query($sql1);
?>

my code on android:

 public static class TestAsyncTask extends AsyncTask<String, Integer, String> {
 protected String doInBackground(String... urls) {
 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
 nameValuePairs.add(new BasicNameValuePair("admin", admin));
 nameValuePairs.add(new BasicNameValuePair("groupname", groupname));
 for (int i=0;i<8;i++)
 {
 nameValuePairs.add(new BasicNameValuePair("groupmates",groupmates[i]));
 }
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://192.168.1.102/webservice/issertgroup.php");
 try {
 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 httpclient.execute(httppost);
 } catch (UnsupportedEncodingException e) {
 e.printStackTrace();
 } catch (ClientProtocolException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }
 return null;
 }

question: $groupmates[0] is null. how can i get the array passing from the android??

i fail with using this method. please help me to solve this. i have searched a lot of references in google, but still cannot solve my problem.

Ravi Dhoriya ツ
4,4148 gold badges39 silver badges48 bronze badges
asked Jan 19, 2015 at 12:06
1
  • new ArrayList<NameValuePair>(2); is wrong it should be 10 as you have for loop 8 times and other 2 Commented Jan 19, 2015 at 12:11

1 Answer 1

1

Method 1

Pass parameter as array element from android/java, as below.

for (int i=0;i<8;i++)
{
 nameValuePairs.add(new BasicNameValuePair("groupmates["+i+"]",groupmates[i]));
}

It will simply pass an array in POST, so in PHP you can get it as below:

<?php
$groupmates=$_POST['groupmates'];
// echo $groupmates[0];
// echo $groupmates[1]; <--process your elements as normal array.
?>

Method 2:

You should use json encoded string of your array. (I'm not an android guy, so can't provide snippet to convert array to json but its easy task, you probably should knew it.)

nameValuePairs.add(new BasicNameValuePair("groupmates",jsonGroupmates));

and in php use following:

$groupmates=json_decode($_POST['groupmates']);

That should work.

answered Jan 19, 2015 at 12:10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.