2015-05-26 17:47
浏览 78

android php mysql从app插入多行到数据库数据库

My app holds a list of student id numbers, their names and grades, and I want to insert the entire list into a database. Each student should be in a row (id-name-grade, in one row).

I am using AsyncTask to send my info to the DB, and wanted to ask how can I send multiple rows (each one containing multiple variables)at once? If I need to send them all as one JSONObject how do I separate them back into the different students?

The tutorials I use to create my app:

I have also found previous answers that deal with the php side of things but as I am new to php I didn't really get it inserting multiple mysql rows php Android

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongqiang4986
    dongqiang4986 2015-05-26 18:46

    On your server side, send the data as an array of JSON objects, then insert objects in a foreach loop, try something like this (this untested, but should get you started):

    //your sample input may look something like this:
    //$json_input_string = '{0:["id":123,"name":"Jon Doe","grade":"A"],1:[...]}';
    $input_data = json_decode($json_input_string);
    foreach($input_data->data as $item)
         //$item->id, $item->name, $item->grade all accessible here
         //insert each record here in loop

    Make sure you are using using prepared statements

    点赞 评论
  • dttvb115151
    dttvb115151 2015-06-14 18:31

    Ok so pesky grey rabbit answered the php side (thanks for that!), but for some reason encoding the JSONArray on the android side was not really working for me. It is probably not the neatest way to do things but I used @UncleIstvan 's answer to figure it out: INSERT multiple entries from Android -> PHP -> MYSQL

    I deleted the "upload fishes" part of the json_string because (again, for some reason) it wouldn't get decoded. @UncleIstvan if I could upvote you, I would

    点赞 评论