0

目前我正在尝试将 ArrayList 数据发送到 php 服务器,但无法成功。ArrayList 包含以下数据:食物名称、食物价格、数量、备注,它们都存储在对象订单中。是这样的

    Order order = new Order();

    order.setFood_name(fooddetail.getString(TAG_FOODNAME));
    order.setFood_price(fooddetail.getString(TAG_FOODPRICE));
    order.setNumber(Integer.toString(number));
    order.setRemark(remark.getText().toString().trim());

    Global.orderList.add(order);

现在我想将它们传递给 php 服务器,这是我尝试过的代码

    try{
                        httpclient=new DefaultHttpClient();
                        httppost= new HttpPost("http://10.0.2.2/android_user/print.php");
                        nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair("count", Integer.toString(count)));
                        for(int i=0;i<Global.orderList.size();i++)
                           {
                            nameValuePairs.add(new BasicNameValuePair("username", Global.UserID));
                            nameValuePairs.add(new BasicNameValuePair("food_name", Global.orderList.get(i).getFood_name()));
                            nameValuePairs.add(new BasicNameValuePair("food_price", Global.orderList.get(i).getFood_price()));
                            nameValuePairs.add(new BasicNameValuePair("quantity", Global.orderList.get(i).getQuantity()));
                            nameValuePairs.add(new BasicNameValuePair("remark", Global.orderList.get(i).getRemark()));
                           }

                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        ResponseHandler<String> responseHandler = new BasicResponseHandler();
                        final String res = httpclient.execute(httppost, responseHandler);

                        if(res.equalsIgnoreCase("Order successfully sent"))
                        {
                            runOnUiThread(new Runnable() 
                            {
                                public void run() 
                                {
                                    Toast.makeText(OrderListActivity.this,"Order Success", Toast.LENGTH_SHORT).show();
                                }
                            });

                            startActivity(new Intent(OrderListActivity.this, LoginActivity.class));
                        }
                        else if(res.equalsIgnoreCase("Oops! Order failed to submit"))
                        {
                            alertmessage = "Oops! Order failed to submit!";
                            showAlert();
                        }
                        else
                        {
                            alertmessage = res.toString();
                            showAlert();
                        }
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }

这是php端代码

    for($i=0; $i<count($_POST['food_name']);$i++)
    {
    $food_name = $_POST['food_name'][$i];
    $food_price = $_POST['food_price'][$i];
    $quantity = $_POST['quantity'][$i];
    $remark = $_POST['remark'][$i];
    $username = $_POST['username'][$i];

    $result = mysql_query("INSERT INTO orderlist(food_name, food_price, quantity, remark, username) VALUES('$food_name', '$food_price', '$quantity','$remark','$username')");
    }

我的问题是,如何发送多行数据并将它们存储到数据库中?目前我只能存储一行或最后一行数据..

4

2 回答 2

0

这个问题:在 UrlEncodedFormEntity(nameValuePairs) 你的对象状态相同的名称之后,它被设置为最后一个数据:

nameValuePairs.add(new BasicNameValuePair("username", Global.UserID));
nameValuePairs.add(new BasicNameValuePair("food_name", Global.orderList.get(i).getFood_name()));
nameValuePairs.add(new BasicNameValuePair("food_price", Global.orderList.get(i).getFood_price()));
nameValuePairs.add(new BasicNameValuePair("quantity", Global.orderList.get(i).getQuantity()));
nameValuePairs.add(new BasicNameValuePair("remark", Global.orderList.get(i).getRemark()));

解决此问题,您需要为对名称添加前缀(例如:“用户名”+ loopIndexNumber)或发送数据分别为每个发送数据。

于 2013-04-15T12:51:00.893 回答
0

尝试这个:

$food_name = $_POST['food_name'];
$food_price = $_POST['food_price'];
$quantity = $_POST['quantity'];
$remark = $_POST['remark'];
$username = $_POST['username'];

for($i=0; $i<count($_POST['food_name']);$i++)
{
    $result = mysql_query("INSERT INTO orderlist(food_name, food_price, quantity, remark, username) VALUES('$food_name[$i]', '$food_price[$i]', '$quantity[$i]','$remark[$i]','$username[$i]')");
}
于 2013-04-15T10:42:43.350 回答