include("includes/db.php");
$customer=$_SESSION['customer'];
//cart data
$get_cart_product=mysqli_query($con, "SELECT * FROM `cart_data` WHERE
customer_reg_id=".$_SESSION['customer']);
// order_status
$order_status="Pending";
$invoice_no=mt_rand();
$total_array=[];
while($row_fetch_cart=mysqli_fetch_array($get_cart_product)){
$cart_data_id=$row_fetch_cart['cart_data_id'];
$cart_pro_id=$row_fetch_cart['product_id'];
$cart_color_id=$row_fetch_cart['color_id'];
$cart_size_id=$row_fetch_cart['size_id'];
$cart_product_price=$row_fetch_cart['product_price'];
$cart_product_qty=$row_fetch_cart['product_qty'];
$vendor_id=$row_fetch_cart['user_id'];
$customer_reg_id=$row_fetch_cart['customer_reg_id'];
// Fetching product information
$get_product=mysqli_query($con, "SELECT * FROM `products` WHERE product_id=$cart_pro_id");
$row_fetch_products=mysqli_fetch_array($get_product);
$product_id=$row_fetch_products['product_id'];
$product_mrp=$row_fetch_products['product_mrp'];
// Cart Total payable amount
$price=$product_mrp;
$qty=$cart_product_qty;
$total=$price*$qty;
$total_array[]= $total;
?>
<?php }?>
<?php $total_amount=array_sum($total_array); echo $total_amount;?>"
//now I want to insert whole fetch data from cart_data table to
customer_orders below is my code. but I'm able to insert to only last row
data
//insert orders
$customer_order_insert="INSERT INTO `customer_orders` (`customer_id`,
`vendor_id`, `product_id`, `invoice_no`, `total_order`, `paid_amount`,
`order_status`, `order_date`) VALUES ('$customer_reg_id', '$vendor_id',
'$cart_pro_id', '$invoice_no', '$cart_product_qty', '$product_mrp',
'pending', CURRENT_TIMESTAMP);";
$run_order_insert=mysqli_query($con,$customer_order_insert);
if($run_order_insert){
echo"<script>alert('Order received successfully')</script>";
}else{
echo"<script>alert('Oop! Something wrong')</script>";
}
首先,我从cart_data
表中获取了所有数据,然后计算了所有提取的mrp的总和。现在我要将整个获取的行数据一起插入到customer_orders
表中,但在我的情况或代码中,只有最后一行数据可以插入customer_order表。所以,伙计们请提出如何做到这一点的建议。
答案 0 :(得分:0)
您应该将插入查询放在循环中。这里只会输入最后一条记录。我更新了您的代码,请尝试一下。
include("includes/db.php");
$customer=$_SESSION['customer'];
//cart data
$get_cart_product=mysqli_query($con, "SELECT * FROM `cart_data` WHERE
customer_reg_id=".$_SESSION['customer']);
// order_status
$order_status="Pending";
$invoice_no=mt_rand();
$run_order_insert=0;
$total_array=[];
while($row_fetch_cart=mysqli_fetch_array($get_cart_product)){
$cart_data_id=$row_fetch_cart['cart_data_id'];
$cart_pro_id=$row_fetch_cart['product_id'];
$cart_color_id=$row_fetch_cart['color_id'];
$cart_size_id=$row_fetch_cart['size_id'];
$cart_product_price=$row_fetch_cart['product_price'];
$cart_product_qty=$row_fetch_cart['product_qty'];
$vendor_id=$row_fetch_cart['user_id'];
$customer_reg_id=$row_fetch_cart['customer_reg_id'];
// Fetching product information
$get_product=mysqli_query($con, "SELECT * FROM `products` WHERE product_id=$cart_pro_id");
$row_fetch_products=mysqli_fetch_array($get_product);
$product_id=$row_fetch_products['product_id'];
$product_mrp=$row_fetch_products['product_mrp'];
// Cart Total payable amount
$price=$product_mrp;
$qty=$cart_product_qty;
$total=$price*$qty;
$total_array[]= $total;
//insert orders
$customer_order_insert="INSERT INTO `customer_orders` (`customer_id`,
`vendor_id`, `product_id`, `invoice_no`, `total_order`, `paid_amount`,
`order_status`, `order_date`) VALUES ('$customer_reg_id', '$vendor_id',
'$cart_pro_id', '$invoice_no', '$cart_product_qty', '$product_mrp',
'pending', CURRENT_TIMESTAMP);";
$run_order_insert=mysqli_query($con,$customer_order_insert);
?>
<?php }?>
<?php $total_amount=array_sum($total_array); echo $total_amount;?>"
//now I want to insert whole fetch data from cart_data table to
customer_orders below is my code. but I'm able to insert to only last row
data
if($run_order_insert){
echo"<script>alert('Order received successfully')</script>";
}else{
echo"<script>alert('Oop! Something wrong')</script>";
}