dongshuobei1037
2016-10-27 09:02
浏览 42
已采纳

使用PHP从数组中插入mysql中的值

I am having a bit of struggle with this. Hope some can guide me a little.

I am trying to take the shopping cart basket products which are in mysql(and not sessions in this case) and move them to an "order-details" table at the checkout.

The array of product is created ok and output the keys and values properly but my problem is when I try to insert the array values into the other table.

Here is the code:

$cart_products = array(); 
$stmt = $conn->prepare("SELECT 
SUM(co.cart_quantity) AS quantity, 
p.product_id, 
p.product_name, 
SUM(p.product_price) AS price, 
p.short_description, 
SUM(p.product_weight) AS weight, 
p.vat 

FROM cart_orders AS co 
LEFT JOIN products AS p 
ON co.cart_product_id = p.product_id 
LEFT JOIN vat_rates AS vr 
ON p.vat = vr.vat_id 
WHERE cart_user_id = ? 
GROUP BY co.cart_product_id 
ORDER BY co.cart_product_id"); 


$stmt->bind_param('i', $user_id); 
$stmt->bind_result($quantity,$p_product_id,$p_product_name,$price,$p_short_description,$weight,$p_vat); 
$stmt->execute(); 
$stmt->store_result(); 
if($stmt->num_rows() > 0){ 
while ($stmt->fetch()) { 

$cart_products[] = array( 
    "product_id" => $p_product_id, 
    "product_name" => $p_product_name, 
    "product_quantity" => $quantity, 
    "product_price" => $price, 
    "vat_id" => $p_vat 
); 
}} 
$stmt->free_result(); 
$stmt->close(); 


$stmt = $conn->prepare("INSERT INTO order_details(product_id,product_name,product_quantity,product_price,vat_id)
VALUES (?,?,?,?,?)");
$stmt->bind_param('isiii', $cart_products['product_id'],$cart_products['product_name'],$cart_products['product_quantity'],$cart_products['product_price'],$cart_products['vat_id']);
$stmt->execute();
$stmt->close();  

This is the output of my array $cart_products:

Array 
( 
    [0] => Array 
        ( 
            [product_id] => 5 
            [product_name] => Product A 
            [product_quantity] => 20 
            [product_price] => 2.50 
            [vat_id] => 2 
        ) 

    [1] => Array 
        ( 
            [product_id] => 7 
            [product_name] => Product A 
            [product_quantity] => 10 
            [product_price] => 2.50 
            [vat_id] => 1 
        ) 

    [2] => Array 
        ( 
            [product_id] => 9 
            [product_name] => Product A44544 
            [product_quantity] => 3 
            [product_price] => 2.50 
            [vat_id] => 2 
        ) 

)  

Thank you in advance for your help!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dronthpi05943 2016-10-27 09:48
    最佳回答

    You have to iterate over your $cart_products to access each product.

    $cart_products = array();
    if ($stmt->num_rows() > 0) {
        while ($stmt->fetch()) {
            $cart_products[] = array(
                "product_id" => $p_product_id,
                "product_name" => $p_product_name,
                "product_quantity" => $quantity,
                "product_price" => $price,
                "vat_id" => $p_vat
            );
        }
    }
    $stmt->free_result();
    $stmt->close();
    
    
    $stmt = $conn->prepare("INSERT INTO order_details(product_id,product_name,product_quantity,product_price,vat_id)
    VALUES (?,?,?,?,?)");
    
    foreach ($cart_products as $cart_product) {
        $stmt->bind_param('isiii', $cart_product['product_id'], $cart_product['product_name'], $cart_product['product_quantity'], $cart_product['product_price'], $cart_product['vat_id']);
        $stmt->execute();
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题