简体   繁体   中英

How to update multiple rows in table

I'm making a online store shopping cart using PHP and MySQL. And now I'm working on cart. Basically till now I can retrieve data from db and show it in the cart just like this image:

捕获

And now I'm working on updating the quantity of products. For example a user must be able to set the quantity of product 1 to 2, product 2 to 3 and product 3 to 4.

So there must multiple queries because each product has a unique product id.

So what I did was this:

<select name='quantities[]'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select>

And the action of this form goes like this:

$cart_id = $_GET['cart_id'];
if(isset($_POST['update'])){
    foreach($_POST['quantities'] as $quantities)
    {
        $insert_qty = "UPDATE cart SET qty = '$quantities' WHERE cart_id = '$cart_id'";
        $run_qty = mysqli_query($con,$insert_qty);
        if($run_qty)
        {
            echo '<META HTTP-EQUIV="Refresh" Content="0; URL=cart.php">';    
        }else{
            die(mysqli_error($con));
        }
    }
}

But the problem with this code is that it only gets the last quantity of last product, and updates the whole shopping cart items to that number.

For example if you set the quantity of last product to 5, it will eventually set the number 5 to all of products existing in that cart.

So my question is how can I be able to update the cart correctly so each product has a separated quantity amount.

I really appreciate any idea, suggestion or solution to this question,

Thanks in advance...

And also here is how I retrieve data (just for looking):

$get_add = "SELECT * FROM cart WHERE cart_id = '$cart_id'";
$run_add = mysqli_query($con,$get_add);
$cart_items = [];
$total_price_to_pay = 0;
while ($row_results = mysqli_fetch_array($run_add)){
    $item = array(
        'table_id' => $row_results['table_id'],
        'cart_id' => $row_results['cart_id'],
        'pro_id' => $row_results['product_id'],
        'pro_title' => $row_results['product_title'],
        'pro_price' => $row_results['product_price'],
        'pro_img' => $row_results['product_image'],
        'pro_supplier' => $row_results['product_supplier'],
        'qty' => $row_results['qty'],
        'cart_ip' => $row_results['cart_ip'],
        'pro_total' => $row_results['qty']*$row_results['product_price'],
    );
    $total_price_to_pay +=  $row_results['qty']*$row_results['product_price'];
    $cart_items[] = $item;
}
foreach ($cart_items as $cart) {
        echo $cart['pro_title'] etc ;
}

You need to post product_id with all quantities (Please replace value="<?php echo $item['pro_id']; ?>" with your actual variable which can set correct product_id in hidden field of each cart item/product OR if it is already there then you can remove this hidden field and use that instead):

<input type="hidden" name="product_ids[]" value="<?php echo $item['pro_id']; ?>" />
<select name='quantities[]'>
    <option value='1'>1</option>
    <option value='2'>2</option>
    <option value='3'>3</option>
    <option value='4'>4</option>
    <option value='5'>5</option>
</select>

And then on action side use this product_id in your update query (which also require change in your DB table if product_id is already not there):

$cart_id = $_GET['cart_id'];
if(isset($_POST['update'])){
    foreach($_POST['quantities'] as $index => $quantities)
    {
        $insert_qty = "UPDATE cart SET qty = '$quantities' WHERE cart_id = '$cart_id' AND product_id = '".$_POST['product_ids'][$index]."'";
        $run_qty = mysqli_query($con,$insert_qty);
        if($run_qty) {
            echo '<META HTTP-EQUIV="Refresh" Content="0; URL=cart.php">';    
        } else {
            die(mysqli_error($con));
        }
    }
}

Hope, it will solve your problem...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM