0

I want to pass array of String (itemname) to a php file (PlaceOrder.php) using name value pair. When I click on PlaceOrder button it passes null value in database. Is there any problem in Android code or Php code? My Android code is following

public void onClick(View v) {
        // TODO Auto-generated method stub
        try
        {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/demo/PlaceOrder.php");
              Log.e("log_tag", "connection success ");
              List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            for (int i=0;i<1;i++)
            {
                nameValuePairs.add(new BasicNameValuePair("ItemName[]", String.valueOf

(resultArrItemname[i])));

            }
            Log.e("log_tag", "Name value pair success ");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            Log.e("log_tag", "response sucess ");
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
             showDialog("Order Placed Successfully");

        }

        catch(Exception e)
        {
            Log.e("log_tag", "Error in http connection"+e.toString());
        }
    }

and my PHP code is follwing

<?php

        $itemname[]=$_REQUEST['ItemName[]'];   

        mysql_connect("localhost","root","aaaaaa") or die(mysql_error());
        mysql_select_db("demo") or die(mysql_error());          

        foreach($itemname as $key)
        {
            $sql=mysql_query("Insert into tblorder(itemname) values('$key')");
            while($row=mysql_fetch_array($sql)) 
        $output[]=$row;
             print(json_encode($output));
        }
         mysql_close();
?>
2
  • Try to generalize your question inspite of using name of files that you have Commented Feb 19, 2013 at 6:47
  • Try this and see if it helps at all: "$itemname[]=$_POST['ItemName'];" Commented Feb 19, 2013 at 6:47

3 Answers 3

1

try changing

$itemname[]=$_REQUEST['ItemName[]']; 

to

$itemname[]=$_REQUEST['ItemName']; 
Sign up to request clarification or add additional context in comments.

Comments

0

$itemname[]=$_REQUEST['ItemName[]'];

It is not better way to use.. Try POST or PUT or other input types... Request may be harmful in future development...

Try ---- $itemname[]=$_POST['ItemName'];

Comments

0

Change $itemname[]=$_REQUEST['ItemName[]'];

to

$itemname=$_REQUEST['ItemName']; 

Remove [] from both side

Use query like below :

$sql=mysql_query("Insert into tblorder(itemname) values('".mysql_real_escape_string($key)."')");

mysql_* functions are deprecated, use mysqli_* and PDO

Change :

while($row=mysql_fetch_array($sql)) 
$output[]=$row;

To :

while($row=mysql_fetch_array($sql)) {
   $output[]=$row;
}

Declare $output = array(); just above the foreach loop

i guess you need to print(json_encode($output)); after foreach loop

Comments

Your Answer

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.