目前我正在尝试将一个ArrayList数据发送到php服务器但无法实现。 ArrayList包括以下数据:foodname,foodprice,quantity,remark,它们都存储在一个对象顺序中。它是这样的
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')");
}
我的问题是,如何发送多行数据并将其存储到数据库中?目前我只能存储一行或最后一行数据..
答案 0 :(得分: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]')");
}
答案 1 :(得分: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()));
解决此问题需要为名称对添加前缀(例如:“username”+ loopIndexNumber)或发送数据为每个分别发送数据。