我想使用名称值对将String
(itemname)数组传递给 php 文件(PlaceOrder.php) 。当我单击PlaceOrder 时,它会在数据库中button
传递空值。Android代码或Php代码有什么问题吗?我的 Android 代码如下
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());
}
}
我的 PHP 代码如下
<?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();
?>