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