将购物车的内容保存到数据库(PHP / SQL)

时间:2016-04-01 11:28:33

标签: php html mysql sql database

我目前正在使用sql数据库在PHP中创建一个简单的电子商务网站。网站有一个带有view_cart页面的购物篮,其中显示购物篮的内容,带有邮资小计的订购商品,所有商品都可以看到。  现在我要做的是将订单详细信息保存到sql数据库并将用户重定向到确认购买的页面。我自己尝试使用按钮来实现它,该按钮运行带有查询的脚本但到目前为止没有运气。数据库包含三行transaction_ID (AI)和两行,用于来自购物篮的数据(ID和总数)。任何人都可以帮我解决这个问题?我此刻完全陷入困境。

非常感谢任何帮助:)

<div class="cart-view-table-back">
    <form method="post" action="cart_update.php">
    <table width="100%"  cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>Price</th><th>Total</th><th>Remove</th></tr></thead>
      <tbody>
        <?php
        include("auth.php");
        include_once("config.php");
        require('db.php');
        if(isset($_SESSION["cart_products"])) //check session var
        {
            $total = 0; //set initial total value
            $b = 0; //var for zebra stripe table 
            foreach ($_SESSION["cart_products"] as $cart_itm)
            {
                //set variables to use in content below
                $title = $cart_itm["title"];
                $product_qty = $cart_itm["product_qty"];
                $price = $cart_itm["price"];
                $id = $cart_itm["id"];
                $product_color = $cart_itm["product_color"];
                $subtotal = ($price * $product_qty); //calculate Price x Qty

                $bg_color = ($b++%2==1) ? 'odd' : 'even'; //class for zebra stripe 
                echo '<tr class="'.$bg_color.'">';
                echo '<td><input type="text" size="2" maxlength="2" name="product_qty['.$id.']" value="'.$product_qty.'" /></td>';
                echo '<td>'.$title.'</td>';
                echo '<td>'.$currency.$price.'</td>';
                echo '<td>'.$currency.$subtotal.'</td>';
                echo '<td><input type="checkbox" name="remove_code[]" value="'.$id.'" /></td>';
                echo '</tr>';
                $total = ($total + $subtotal); //add subtotal to total var
            }

            $grand_total = $total + $shipping_cost; //grand total including shipping cost
            foreach($taxes as $key => $value){ //list and calculate all taxes in array
                    $tax_amount     = round($total * ($value / 100));
                    $tax_item[$key] = $tax_amount;
                    $grand_total    = $grand_total + $tax_amount;  //add tax val to grand total
            }

            $list_tax       = '';
            foreach($tax_item as $key => $value){ //List all taxes
                $list_tax .= $key. ' : '. $currency. sprintf("%01.2f", $value).'<br />';
            }
            $shipping_cost = ($shipping_cost)?'Shipping Cost : '.$currency. sprintf("%01.2f", $shipping_cost).'<br />':'';
        }
        ?>
        <tr><td colspan="5"><span style="float:right;text-align: right;"><?php echo $shipping_cost. $list_tax; ?>Amount Payable : <?php echo sprintf("%01.2f", $grand_total);?></span></td></tr>
      </tbody>
    </table>
    <?php
        echo "<a href=\"javascript:history.go(-1)\">Add More Items</a>";
    ?>
    <input type="hidden" name="return_url" value="<?php 
    $current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    echo $current_url; ?>" />
    </form>
</div>

0 个答案:

没有答案