我想使用名称/值对将String(商品名称)数组传递给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();
?>

最佳答案

尝试改变

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




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

关于php - 将数据数组从android传递到php文件不会输入数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14951366/

10-12 00:13
查看更多