How to submit all cart items using a form and insert into a database table

I have some problems while placing an order on my php cart website. I have added cart items successfully, but when I place an order, all cart items should insert my order table into the base. I am using a form to post all values ​​from a cart and insert into a database. The last updated cart item only inserted my database. The rest of the items were not inserted into my database. Someone please help me. Thanks in advance.

My code

foreach ($_SESSION["cart_array"] as $each_item) {

    $item_id = $each_item['item_id'];
    $sql = mysql_query("SELECT * from products WHERE pid='$item_id' LIMIT 1");
    while ($row = mysql_fetch_array($sql)) {
        $product_name = $row["pname"];
        $price = $row["price"];
        $details = $row["description"];
        $image = '<img style="border:#666 1px solid;" src = "admin123/products/' . $item_id . '.jpg" alt="' . $product_name. '" width="50" height="50" border="1" />';
    }
    $pricetotal = $price * $each_item['quantity'];
    $cartTotal = $pricetotal + $cartTotal;

    $cartOutput .='  <form action="cart.php" method="post">
    <div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column">'.$image.'
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column text">
                            <a href="store.php?pid='.$item_id.'">'.$product_name.'</a>
                        </div>

                        <div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column text">
                            <p>Price: '.$price.'&nbsp;€&nbsp;</p>
                        </div>

                        <div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column quantity">
                            <div class="quantity buttons_added pm-checkout-quantity">
                                  <input type="number" size="4" class="input-text qty cart text" title="Qty" value="' . $each_item['quantity'] . '" name="quantity" min="1" step="1" >                    
                            </div><!-- quantity buttons end -->
                        </div>

                        <div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column text">
                            <p>Sub-Total: '.$pricetotal.'&nbsp;€&nbsp;</p>
                        </div>

                        <div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column">
                            <a href="cart.php?index_to_remove='.$i.'" class="pm-rounded-btn pm-primary pm-cart-remove">Remove</a>
                        </div>
                        <input type="hidden" name="order-pname" value="'.$product_name.'">
<input type="hidden" name="order-price" value=" '.$price.'">
<input type="hidden" name="order-qty" value="' . $each_item['quantity'] . '">
<input type="hidden" name="order-total" value="'.$pricetotal.'">
<input type="hidden" name="item-id" value="' . $item_id . '">

                         ';
                         $i++;
                         }

      

and i am closing my form tag between body tag.

And the embed code is below

if(isset($_POST['place-order']))
{
    $pid = $_POST['item-id'];
    $ocname = $_SESSION['fname'];
    $oclname = $_SESSION['lname'];
    $opname=$_POST['order-pname'];
    $oprice=$_POST['order-price'];
    $oqty=$_POST['order-qty'];
    $ototal=$_POST['order-total'];
    $email = $_SESSION['email'];
    $address=$_SESSION['address']; 
    $city=$_SESSION['city'];
    $state=$_SESSION['state'];
    $country=$_SESSION['country'];
    $zip= $_SESSION['zip'];
    $phone=$_SESSION['phone'];
    $sql =mysql_query("INSERT INTO orders (pid, customer_name, customer_lname, product_name, qty, price, total, date_added, customer_email, customer_address, customer_city, customer_state, customer_country, customer_zip, customer_phone ) VALUES('$pid','$ocname','$oclname', '$opname', '$oqty','$oprice','$ototal', now(), '$email','$address','$city','$state','$country','$zip','$phone' ) ") or die(mysql_error());

}

      

So please help me.

+3


source to share


1 answer


The problem is that you only insert one item, although you get all the records, but you don't use a loop, so only the last product is inserted into your database to form your code like this:

foreach ($_SESSION["cart_array"] as $each_item) {

$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * from products WHERE pid='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
    $product_name = $row["pname"];
    $price = $row["price"];
    $details = $row["description"];
    $image = '<img style="border:#666 1px solid;" src = "admin123/products/' . $item_id . '.jpg" alt="' . $product_name. '" width="50" height="50" border="1" />';
}
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;

$cartOutput .='  <form action="cart.php" method="post">
<div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column">'.$image.'
                    </div>
                    <div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column text">
                        <a href="store.php?pid='.$item_id.'">'.$product_name.'</a>
                    </div>

                    <div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column text">
                        <p>Price: '.$price.'&nbsp;€&nbsp;</p>
                    </div>

                    <div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column quantity">
                        <div class="quantity buttons_added pm-checkout-quantity">
                              <input type="number" size="4" class="input-text qty cart text" title="Qty" value="' . $each_item['quantity'] . '" name="quantity" min="1" step="1" >                    
                        </div><!-- quantity buttons end -->
                    </div>

                    <div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column text">
                        <p>Sub-Total: '.$pricetotal.'&nbsp;€&nbsp;</p>
                    </div>

                    <div class="col-lg-2 col-md-2 col-sm-2 pm-cart-info-column">
                        <a href="cart.php?index_to_remove='.$i.'" class="pm-rounded-btn pm-primary pm-cart-remove">Remove</a>
                    </div>
                    <input type="hidden" name="order-pname[]" value="'.$product_name.'">
                    <input type="hidden" name="order-price[]" value=" '.$price.'">
                    <input type="hidden" name="order-qty[]" value="' . $each_item['quantity'] . '">
                    <input type="hidden" name="order-total[]" value="'.$pricetotal.'">
                    <input type="hidden" name="item-id[]" value="' . $item_id . '">

                     ';
                     $i++;
                     }

      

your post and embed code should be as follows:



    if(isset($_POST['place-order']))
{    $allpid = $_POST['item-id'];
    $oqty=$_POST['order-qty'];
    $ototal=$_POST['order-total'];

    $opname=$_POST['order-pname'];
    $oprice=$_POST['order-price'];
    $i=0;
    foreach($allpid as $id)
    {
    $pid=$id;
    $ocname = $_SESSION['fname'];
    $oqty=$oqty[$i];
     $ototal=$ototal[$i];
     $opname=$opname[$i];
    $oprice=$oprice[$i];
    $oclname = $_SESSION['lname'];
    $email = $_SESSION['email'];
    $address=$_SESSION['address']; 
    $city=$_SESSION['city'];
    $state=$_SESSION['state'];
    $country=$_SESSION['country'];
    $zip= $_SESSION['zip'];
    $phone=$_SESSION['phone'];
    $sql =mysql_query("INSERT INTO orders (pid, customer_name, customer_lname, product_name, qty, price, total, date_added, customer_email, customer_address, customer_city, customer_state, customer_country, customer_zip, customer_phone ) VALUES('$pid','$ocname','$oclname', '$opname', '$oqty','$oprice','$ototal', now(), '$email','$address','$city','$state','$country','$zip','$phone' ) ") or die(mysql_error());
    $i=$i+1;
    }
}

      

This is my answer for you in the situation you gave, but I suggest you split tables for data and product data.

0


source







All Articles