Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
购物车/购物篮会话不会进入数据库:PHP MySQL_Php_Mysql_Session_Variables_Shopping Cart - Fatal编程技术网

购物车/购物篮会话不会进入数据库:PHP MySQL

购物车/购物篮会话不会进入数据库:PHP MySQL,php,mysql,session,variables,shopping-cart,Php,Mysql,Session,Variables,Shopping Cart,我正在用php/mysql编写一个程序,在该程序中,登录用户使用积分系统通过购物车订购物品(不涉及金钱、paypal、信用卡付款、运费税等-仅积分)。我的php知识是从初级到中级的 我有以下两个脚本: 名为view_cart.php的购物车/购物篮 这很好(如果他们没有足够的点数,则禁用签出) 订单提交到名为submit\u Order.php的数据库 我在将购物车会话链接到提交订单页面/数据库时遇到问题。我收到第一条错误消息,但它在orders表下创建了一个新订单,但仅使用登录的用户ID号,而

我正在用php/mysql编写一个程序,在该程序中,登录用户使用积分系统通过购物车订购物品(不涉及金钱、paypal、信用卡付款、运费税等-仅积分)。我的php知识是从初级到中级的

我有以下两个脚本:

  • 名为view_cart.php的购物车/购物篮 这很好(如果他们没有足够的点数,则禁用签出)

  • 订单提交到名为submit\u Order.php的数据库

  • 我在将购物车会话链接到提交订单页面/数据库时遇到问题。我收到第一条错误消息,但它在orders表下创建了一个新订单,但仅使用登录的用户ID号,而总数为0。order_contents表也不会发生任何变化

    我猜这与“cart”会话变量/数组有关,因此如果有人能帮助我或引导我朝着正确的方向前进,那就太好了

    谢谢

    查看\u 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');
        ?>
    
    我看不见

    session_start(); 
    

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

    我猜您把
    会话\u start()
    放错地方了。必须在
    标记之前调用它。

    是的,它在头html文件中。它应该是第一个调用文件的第一行。我将在查看购物车上尝试此操作,并添加购物车页面,让您知道它是如何运行的。这意味着你把它放在第一个“会话启动()之后”;应该是在我拥有登录用户的会话用户id之后的第一件事,这样只有登录用户才能看到这个脚本,那么bean会出现问题吗?
    session_start();