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

使用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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题