Foreach循环为每个产品ID运行查询

时间:2013-03-14 14:56:59

标签: php mysql arrays multidimensional-array

我有一个多维数组,我创建了一个foreach循环并将数据存储到单独的变量中。我相信我还需要在foreach之上创建另一个$query2循环,因此对于存储在数组中的每个产品ID,我运行查询。但我不确定如何使这个工作?

function createTransaction($mem_id, $member_data, $cart_array){
$query1 = mysql_query("INSERT INTO `transactions` (mem_id, OrderDate, ship_phone, ship_address, ship_city, ship_county, ship_postcode, ship_country) VALUES('{$_SESSION['mem_id']}', NOW(), '{$member_data['mem_tel']}', '{$member_data['mem_address']}', '{$member_data['mem_city']}', '{$member_data['mem_county']}', '{$member_data['mem_postcode']}', '{$member_data['mem_country']}')") or die(mysql_error());
if($query1) {
    // Get the highest ID in the transactions table. This should be the ID of the row we just inserted.
    $tempInfo = mysql_query("SELECT `order_id` FROM `transactions` ORDER BY `order_id` DESC LIMIT 1");
    $tempInfo = mysql_fetch_assoc($tempInfo);
    $orderId = $tempInfo['order_id'];

    // Insert into the transaction details table.

    // Insert into the transactions table
    foreach ($_SESSION['cart_array'] as $k => $v) {
        $item_id_transaction = $_SESSION['cart_array'][$k]['item_id'];
        $item_price_transaction = $_SESSION['cart_array'][$k]['price'];
        $item_quantity_transaction =  $_SESSION['cart_array'][$k]['quantity'];

        $query2 = mysql_query("INSERT INTO `transactionDetails` (Order_ID, Product_ID, Price, Quantity) VALUES('{$orderId}', '{$item_id_transaction}', '{$item_price_transaction}', '{$item_quantity_transaction}')");
    }
    if($query2) {
        // Success.
        echo 'success';
    } else {
        // Error occurred.
        echo 'Query2 Error: ' . mysql_error();
    }
} else {
    // Error occurred.
    echo 'Query1 Error: ' . mysql_error();
}
}

0 个答案:

没有答案