Hi,
I'm having real trouble making sense of strings passed back from a credit card processor to my site via <input> tags.
If the customer buys more than one product, I get the following in the passback:
<input type="hidden" name="product_id" value="2" />
<input type="hidden" name="product_id1" value="1" />
<input type="hidden" name="quantity" value="5" />
<input type="hidden" name="quantity1" value="3" />
and so on.
I need to match 'quantity1' to 'product_id1' etc. and then input them into the MySQL database into a table called 'orders'. Each product order gets its own row, but is linked by the client_id.
I have set up the following 'foreach' loop
foreach($_POST as $key => $value)
{
$title = preg_replace('/[^a-z,A-Z]/','',$key);
$str = "$title[0]$title[1]$title[2]$title[3]$title[4]$title[5]$title[6]$title[7]$title[8]";
$str2 = "$title[0]$title[1]$title[2]$title[3]$title[4]$title[5]$title[6]$title[7]";
if ($str == 'productid') {
$product_id = $value;
$num = preg_replace('/[^0-9]/','',$key);
} elseif ($str2 == 'quantity') {
$quantity = $value;
$query4 = "INSERT into orders (client_id, product_id, order_number, quantity, date) VALUES ('$client_id', '$product_id', '$order_number', '$quantity', NOW())";
$result4 = @mysql_query ($query4);
if ($result4) {
$query5 = "UPDATE products SET quantity_sold = quantity_sold + '$quantity' WHERE product_id = $product_id";
$result5 = @mysql_query ($query5);
}
}
}
mysql_close();
}
This works fine when the <input> tags come in the following order:
<input type="hidden" name="product_id" value="2" />
<input type="hidden" name="quantity" value="5" />
<input type="hidden" name="product_id1" value="1" />
<input type="hidden" name="quantity1" value="3" />
But I don't have control over the order the 3rd party shopping cart sends them in!
Although the quantity seems to work, the problem is that it seems to input the same product_id into all the rows, depending on which product_id is processed first. This also means the quantity in the products row is wrong.
Does anyone have an idea how I can keep the loop but match the relevant 'product_id's with 'quantity's?
I'd be really grateful for any help.
All the best,
Rob