购物车/购物篮会议不去数据库:PHP MySQL

时间:2011-05-03 00:51:00

标签: php mysql session variables shopping-cart

我正在用php / mysql编写一个程序,其中登录用户使用积分系统通过购物车订购商品(不涉及金钱,PayPal,信用卡付款,运费等等 - 仅限积分)即可。我的PHP知识是低中级的基础。

我有两个脚本:

  1. 名为 view_cart.php 的购物车/购物篮 这样可以正常工作(如果没有足够的点,则禁用结账)。

  2. 向数据库提交订单 submit_order.php

  3. 我在将购物车会话链接到提交订单页面/数据库时遇到问题。我收到了第一条错误消息,但它在订单表下创建了一个新订单,但只有登录的用户ID号,而总数达到0. order_contents表也没有任何反应。

    我猜这与'cart'会话变量/数组有关,所以如果有人可以帮助或引导我朝着正确的方向发展,那就太好了。

    谢谢A

    view_cart.php 如下:

        <?php //view_cart.php
        $page_title = 'ViewCart';
        include ('./includes/header.html');
    
        if (!isset($_SESSION['users_id'])) {
    
           $url = 'http://' . $_SERVER['HTTP_HOST']
            . dirname($_SERVER['PHP_SELF']);
           if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
                $url = substr ($url, 0, -1); 
           }
    
           $url .= '/login.php'; 
    
        ob_end_clean(); 
        header("Location: $url"); 
        exit(); 
        }
    
        $rwp = $_SESSION['points'];    
    
        $problem = FALSE; 
    
    
        if (isset($_POST['submitted']))
           { 
    
        foreach ($_POST['qty'] as $k => $v) {
    
        $pid = (int) $k;
        $qty = (int) $v;
    
        if ( $qty == 0 ) { 
        unset ($_SESSION['cart'][$pid]);
        } elseif ( $qty > 0 ) { 
        $_SESSION['cart'][$pid] ['quantity'] = $qty;
        }
    
        } 
        } 
    
        $empty = TRUE;
        if (isset ($_SESSION['cart'])) {
        foreach ($_SESSION['cart'] as $key =>$value) {
        if (isset($value)) {
        $empty = FALSE;
        break; 
        }
        } 
        } 
    
        if (!$empty) {
    
        require_once ('mysql_connect.php');
    
    
        $query = "SELECT users_id, points FROM user_points
                    WHERE user_points.users_id = users.users_id";
        $result = mysql_query($query);  
    
    
        $query = "SELECT products_id, products_name FROM categories, products
           WHERE categories.categories_id = products.categories_id AND products.products_id 
           IN (";foreach ($_SESSION['cart'] as $pid =>$value) {
        $query .= $pid . ',';
        }
        $query = substr ($query, 0, -1) . ') ORDER BY categories.categories_name ASC';
        $result = mysql_query($query);
    
    
        echo '
        <table border="0" width="100%" cellspacing="1" cellpadding="5"
           align="center">
        <tr class="top">
        <td align="left" width="46%"><b>Product</b></td>
        <td align="right" width="18%"><b>Price</b></td>
        <td align="center" width="16%"><b>Qty</b></td>
        <td align="right" width="20%"><b>Sub Total</b></td>
        </tr>
        <form action="view_cart.php" method="post">
        ';
    
        $total = 0; 
    
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    
        $subtotal = $_SESSION['cart'][$row
           ['products_id']]['quantity'] *
           $_SESSION['cart'][$row ['products_id']]['price'];
        $total += $subtotal;
    
        echo " <tr>
        <td align=\"left\">{$row['products_name']}</td>
        <td align=\"right\">{$_SESSION['cart'][$row['products_id']] ['price']} pts</td>
        <td align=\"center\"><input type=\"text\" size=\"3\"
           name=\"qty[{$row['products_id']}]\"
           value=\"{$_SESSION['cart'][$row['products_id']]['quantity']}\" /></td>
        <td align=\"right\">" . number_format ($subtotal) . " pts</td>
        </tr>\n";
        } 
    
        mysql_close($dbc); 
    
        $str = '<tr class="even">
        <td colspan="3" align="right"><b> TOTAL:<b></td>
        <td align="right"><b>' . number_format ($total) . ' pts </b></td>
        </tr>
        </table>
        <br />
        <div align="center"><input type="submit" name="submit" value="Update" />
        <input type="hidden" name="submitted" value="TRUE" />
        </form><br /><br /></div>';
        if($up >= $total) {
         $str .='<a href="submit_order.php">Submit Order</a></p>';
        }
        else {
         $str .='<p>You do not have enough points to proceed to checkout</p>'; 
        }   
        echo $str;
    
        } else {
        echo '<p>Your cart is currently empty.</p>';
        }
        ?>
    
        <?php
        include ('./includes/footer.html');
        ?>
    

    以下是 submit_order.php 脚本。

        <?php 
        $page_title = 'Order Confirmation';
        include ('./includes/header.html');
    
        if (!isset($_SESSION['users_id'])) {
    
           $url = 'http://' . $_SERVER['HTTP_HOST']
            . dirname($_SERVER['PHP_SELF']);
           if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
                $url = substr ($url, 0, -1); 
           }
           $url .= '/login.php'; 
    
        ob_end_clean(); 
        header("Location: $url"); 
        exit(); 
        }
    
        $users = $_SESSION['users_id']; // Temporary.
    
        $total = 0;
    
        require_once ('mysql_connect.php'); // Connect to the database.
    
        @mysqli_autocommit ($dbc, FALSE);
    
        $query = "INSERT INTO orders (users_id, total) VALUES 
            ($users, $total)";  
        $result = @mysql_query($query);
        if (@mysql_affected_rows($dbc) == 1) {
    
        $oid = @mysql_insert_id();
    
        $query = "INSERT INTO order_contents (order_id, products_id, quantity, price)
           VALUES ";
        foreach ($_SESSION['cart'] as $pid => $value) {
        $query .= "($oid, $pid, {$value['quantity']}, {$value['price']})";
        }
        $query = substr($query, 0, -2); 
        $result = @mysql_query($query);
    
        if (@mysql_affected_rows($dbc) == count($_SESSION['cart'])) { 
    
        @mysqli_commit($dbc);
        @mysql_close($dbc);
    
        unset($_SESSION['cart']);
    
        echo '<p>Thank you for your order.
           It has been submitted for processing.</p>';
    
        } else { 
    
        @mysqli_rollback($dbc);
        @mysql_close($dbc);
    
        echo '<p>Your order could not be processed due to a system error.
           You will be contacted in order to have the problem fixed.
           We apologize for the inconvenience 1.</p>';
        }
        }
    
        else { 
    
        @mysqli_rollback($dbc);
        @mysql_close($dbc);
    
        echo '<p>Your order could not be processed due to a system error.
           You will be contacted in order to have the problem fixed.
           We apologize for the inconvenience 2.</p>';
        }
    
        ?>
        </div></div>
    
    
        <?php  
        include ('./includes/footer.html');
        ?>
    

2 个答案:

答案 0 :(得分:0)

我看不到

session_start(); 

在这里的任何地方,你是从包含文件的父母那里调用它吗?

答案 1 :(得分:0)

我猜你把session_start()放在了错误的地方。必须在<html>标记之前调用它。