Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:00:39+00:00 2026-05-27T05:00:39+00:00

new guy here! I am building a custom shopping cart driven by mysql and

  • 0

new guy here!
I am building a custom shopping cart driven by mysql and i am trying to update my cart item by item in terms of quantity. It seems that i am doing something wrong because when i try to update the quantity it only update the last item. I am posting the code below. Any help would be appreciated. Thanks in advance.

1.cart.php:

    $sql = "select * from orders";
    $result = mysql_query($sql);
    $num = mysql_num_rows($result);
    echo "Στοιχεία: ".$num;
    ?>
    <form name="cart" method="post" action="updatecart.php">
      <table border="2">
        <tr>
          <td>Α/Α</td>
          <td>img_name</td>
          <td>Reprints</td>
          <td>Color</td>
        </tr>
        <?php
        if($num){
            while ($row = mysql_fetch_array($result)){
             ?>
                <tr>
                  <td><?php  echo $row['item_id']; ?></td>
                  <td><?php echo $row['img_name']; ?></td>
                  <td><input type="number" name="quantity" value="<?php echo $row['quantity']; ?>"></td>
                  <input type="hidden" name="item_id" value="<? echo $row['item_id']; ?>">
                  <td><?php echo $row['color']; ?></td>
                </tr>
            <?php
            }
        }

            ?>
      </table>
      <input type="submit" name="update" value="Update Cart" />
      <input type="button" name="2checkout" value="Proceed to Checkout" />
</form>

2.updatecart.php

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<?php
    $database_name = "vog";
    $conn = mysql_connect("localhost","root","toor");
    mysql_select_db($database_name);

    $num = 2; //na to ferw me session meta edw!
    if(isset($_POST['update'])){
        $item_id = $_POST['item_id'];
        $i=1;

        while($i<=$num){
            $item_id = $_POST['item_id'][$i];
            $quantity = $_POST['quantity'];
            $sql2 = "update orders SET quantity = '$quantity' where item_id = '$item_id' ";
            $result2 = mysql_query($sql2) or die ("Error in query: $result2");
            $i++;
        }
    }

    if(isset($result2)){
        header("Location:cart.php");
    }
?>

So far it updates just the last record.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:00:39+00:00Added an answer on May 27, 2026 at 5:00 am

    in #1.cart.php use the inputs as array:

    <input type="number" name="quantity[<?php  echo $row['item_id']; ?>]" value="<?php echo $row['quantity']; ?>">
    <input type="hidden" name="item_id[<?php  echo $row['item_id']; ?>]" value="<? echo $row['item_id']; ?>">
    

    and in #2.updatecart.php: process it like

     foreach($_POST['item_id'] as $key => $id){
    
     $item_id = $id;
     $quantity = $_POST['quantity'][$key];
     $sql2 = "update orders SET quantity = '$quantity' where item_id = '$item_id' ";
     $result2 = mysql_query($sql2) or die ("Error in query: $result2");
     $i++;
    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey everyone, a new guy here in C#.Net. I'm trying to make an application
new guy here and I'm slowly getting the hang of python, but I have
I'm a new Ruby/Rails guy. Here's one question puzzling me: Can we find the
New guy here so bear with me. Ive got a basic XSL file that
i am a totally new guy around here and i dont know whether its
So I am an old web forms guy and new to the MVC (in
I have never developed for WAMP, the hosting guy for my new client says
I am a C# guy trying to translate some of my OOP understanding over
Here is my scenario. I'm am HTML/CSS guy, JavaScript not so much. But this
Business Intelligence guy here with enough C# under my belt to be dangerous. I’ve

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.