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

我的应用程序包含学生ID号码,姓名和成绩列表,我想将整个列表插入 一个数据库。 每个学生应该连续(id-name-grade,in a row)。 </ p>

我正在使用AsyncTask将我的信息发送到数据库,并想问我如何一次发送多行(每行包含多个变量)? 如果我需要将它们全部作为一个JSONObject发送,我该如何将它们分成不同的学生? </ p>

我用来创建我的应用程序的教程:
http://www.mybringback.com/android-sdk/13193/android-mysql-php-json-part-5-developing -the-android-application /
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ </ p>

我也有 发现以前的答案处理PHP方面的东西,但因为我是新来的PHP我没有真正得到它插入多个mysql行php Android </ p>
</ div>

展开原文

原文

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: http://www.mybringback.com/android-sdk/13193/android-mysql-php-json-part-5-developing-the-android-application/ http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

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个回答

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

<?php
//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

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐