Reputation: 55
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(R.id.textView1);
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 ) {
@die(@mysqli_error());
}
$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;
}
}
$mysqli->close();
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
Cheers
Upvotes: 0
Views: 249
Reputation: 91
I'm not an android developer, but I can help you debug it in your PHP code. First of all, can you do a var_dump($_POST) and tell me the output?
I can see that you're using json_decode wrong, it looks like you're trying to use it for each key value pair instead of the full json text. The way it works (check http://php.net/manual/en/function.json-decode.php for more information is that you pass it a full json text (i.e. '{ "a":1, "b":2 }') and it translates it to POPO (Plain old PHP object) that can be accessed this way
$object = json_decode('{ "a":1, "b":2 }');
$a = $object->a; // $a = 1
$b = $object->b; // $b = 2
Upvotes: 1
Reputation: 1635
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.
Upvotes: 1