如何使用表单发送所有购物车项目并插入数据库表

时间:2015-05-19 06:23:12

标签: php mysql database

我在php购物车网站下订单时遇到了一些问题。我成功添加了购物车商品,但是当我下订单时,所有购物车商品都需要插入我的数据库订单表。我正在使用表单从购物车发布所有值并插入数据库。最后更新的购物车项目仅插入了我的数据库。其余项目未插入我的数据库。有人请帮帮我。提前谢谢。

我的代码是

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++;
                         }

我正在我的身体标签之间关闭我的表格标签。

插入代码在

下面
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());

}

所以有人请帮助我。

1 个答案:

答案 0 :(得分:0)

问题是你只插入单个项目,虽然你获得了所有记录,但是你没有使用循环,所以只有最后一个产品被插入你的数据库,所以你的代码合成如下!

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++;
                     }

您的帖子和插入代码必须如下!

    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;
    }
}

在您给出的情况下,这是我的答案,但我建议您将表格与客户数据和产品数据分开。