0

Currently i am trying to send an ArrayList data to php server but cannot make it. the ArrayList include data which are: foodname, foodprice, quantity, remark and they all store inside an object order. it is something like this

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

and now i want to pass them into php server, this is the code i tried

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

and this is the php side code

    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')");
    }

My question is, how to send multiple rows of data and store them in to database? currently i am able to store only one row or the last row of data..

Liang Neo
  • 3
  • 2
  • 4

2 Answers2

0

try this:

$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]')");
}
pulsar
  • 986
  • 1
  • 9
  • 22
  • I tried this before but it will only take the first row of data and the first character send to database.. – Liang Neo Apr 15 '13 at 12:55
0

This problem: after UrlEncodedFormEntity(nameValuePairs) your objects state same name, it is set last data:

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

Solution this problem you need add prefix to pair name (Example: "username" + loopIndexNumber) or send data send your data separately for each.

SBotirov
  • 13,872
  • 7
  • 59
  • 81