Jolle Jolle - 6 months ago 18x
PHP Question

how to read json array in php

i am trying to read my android-data in php and insert it into two different tables (one table with common data and one table with every item in array). Inserting the common data into the orders table works fine, the array data does not.

Here is my android-code with my basicNameValuePairs:

ArrayList<ShoppingCardArticle> shoppingCardArticles = UsingSharedPrefs.getShoppingCard();

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

TextView time = (TextView)findViewById(;
String stringTime = time.getText().toString();
int amount = shoppingCardArticles.size();
String sAmount = Integer.toString(amount);
double fullprice = 0;
for(int a=0;a<shoppingCardArticles.size();a++) {
double price = shoppingCardArticles.get(a).getArticlePrice();
int quant = shoppingCardArticles.get(a).getArticleQuantity();
price = price * quant;
fullprice = fullprice + price;
String sFullprice = Double.toString(fullprice);
nameValuePairs.add(new BasicNameValuePair("fullprice", sFullprice));
nameValuePairs.add(new BasicNameValuePair("amount", sAmount));
nameValuePairs.add(new BasicNameValuePair("time", stringTime));

for(int i = 0; i<shoppingCardArticles.size(); i++) {
String proid = Integer.toString(shoppingCardArticles.get(i).getArticleId());
String proquan = Integer.toString(shoppingCardArticles.get(i).getArticleQuantity());

nameValuePairs.add(new BasicNameValuePair("proid[]", proid));
nameValuePairs.add(new BasicNameValuePair("proquan[]", proquan));

JSONParser jsonParser = new JSONParser();
JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", nameValuePairs);
return null;

And this is my php-code:

$lastIdSql = "SELECT orderID FROM orders ORDER BY orderID DESC LIMIT 1";
$lastID = mysqli_query( $mysqli, $lastIdSql );
if ( ! $lastID ) {
$lastIDi = mysqli_fetch_array($lastID);
$lastIDii = $lastIDi['orderID'];
$ordDynID = $lastIDii + 1;

$fullprice = $_POST['fullprice'];
$amount = $_POST['amount'];
$time = $_POST['time'];

$queryOrd = "INSERT INTO orders (userID, fullprice, orderTime, amount, ordDynID) VALUES ('1', '$ordFullPrice', '$ordTime', '$ordAmount', '$ordDynID')";

if ($mysqli->query($queryOrd) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $queryOrd . "<br>" . $mysqli->error;

$proID = array($_POST['proid']);
$proQuan = array($_POST['proquan']);

foreach($proID as $item) {
$productid = $item['proid'];
$productquantity = $item['proquan'];

$query = "INSERT INTO ordersproducts (ordID, proID, proQuan) VALUES ('$ordDynID', '$productid', '$productquantity')";

if ($mysqli->query($query) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . $mysqli->error;

So my question is, am i doing something wrong while reading the data in my php-file or is it a problem in my android-code?

Thanks in advance



When you post arrays by name in your Android code like proID[] and proQuan[]. The $_POST['proid'] and $_POST['proquan'] are already arrays. It's similar to a html form having several inputs with the name with square brackets ie.(<input type="text" "name="proid[]" value="" />) So you just assign it like this.

$proID = $_POST['proid'];
$proQuan = $_POST['proquan'];

And assuming the elements in $proID and $proQuan are correctly mapped to each other. A for loop is more suitable to iterate through the arrays.

for ($i = 0; $i < count($proID); $i++) {
    $productid = $proID[$i]; 
    $productquantity = $proQuan[$i];
    //your insert goes here


And of course I should tell you about the SQL Injection vulnerability in your PHP code. Use prepared statement and parameter binding to prevent that.