1

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 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.