PHP购物车,将商品增加到购物车故障

时间:2015-04-22 00:40:20

标签: php

我已连接到数据库。

我有一个在PHP程序中工作的购物车,问题是:
如果我点击将item1添加到购物车,它将添加它,如果我再次按它,​​它将再次添加它。然后,如果我按添加到项目2上的购物车,它将再次添加项目1 但不添加项目2,然后如果我再次按项目2,它将开始增加项目2购物车。我上传了我使用它的2个页面,因为我不确定代码的哪个部分导致问题。

 <?php
    // add before
    session_start();

    // connect
    require_once("include/db_connect.php");

    //
    if(isset($_REQUEST['page']) ){
        $pages = array("products","cart"); 

        if(in_array($_REQUEST['page'], $pages) ){
            $page = $_REQUEST['page'];
        }
        else{

            $page = "products";
        }
    }
    else{
        $page = "products";
    }

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8" />
</head>

<body>
    <div id="sect">


                <div style="float:right; width:300px; border:1px solid red;">
            <h1>Cart</h1>
            <?php
                if(isset($_SESSION['cart']) ){

                    $sql = "SELECT * FROM product WHERE id_number IN(";
                        foreach($_SESSION['cart'] as $id => $value){
                            $sql .= $id. ",";
                        }

                        // will take away the last comma
                    $sql = substr($sql,0,-1) . ") ORDER BY id_number ASC";
                    $query = mysql_query($sql) or die(mysql_error() );



                while($row = mysql_fetch_assoc($query)){
            ?>

                <p><?php echo $row['name']; ?> <?php echo " " . $_SESSION['cart'][$row['id_number']]['quantity']; ?> </p>


            <?php
                }

                }else{
                    echo " <p>Your cart is empty. <br> Please add some products</p> ";

                }

                echo "<a href='index.php?page=cart'>Go to Cart</a> ";
            ?>
        </div>

        <?php require($page . ".php"); ?>


    </div> 
</body>
</html>

这是另一个类:

    <?php

    if(isset($_REQUEST['action']) && ($_REQUEST['action'] == "add" )){

                 $id = intval($_REQUEST['id']);

                 if(isset($_SESSION['cart'][$id]) ){
                     $_SESSION['cart'][$id]['quantity']++ ;
                 }
                 else{
                     $sql2 = "SELECT * FROM product WHERE id_number={$id}";
                     $query2 = mysql_query($sql2);

                     if(mysql_num_rows($query2) != 0 ){
                        $row2 = mysql_fetch_array($query2);
                        $_SESSION['cart'][$row2['id_number']] = array("quantity" => 1, "price" => $row2['price'] ) ;
                     }
                     else{
                            $message =  "This product id is invalid" ;
                     }
                 }
    }

?>


<h2 class="message"><?php if(isset ($message)){echo $message;} ?></h2>
<h1>Product Page</h1>
<!-- 297de5 -->
<table>
    <tr>
        <th>name</th>
        <th>artist</th>
        <th>price</th>
        <th>Action</th>
    </tr>

    <?php

        $sql = "SELECT * FROM product ORDER BY price ASC";
        $query = mysql_query($sql) or die(mysql_error() );

        while($row = mysql_fetch_assoc($query) ){

    ?>

    <tr>
        <td><?php echo $row['name']; ?> </td>
        <td><?php echo $row['artist']; ?> </td>
        <td>$<?php echo $row['price']; ?> </td>
        <td><a href="index.php?page=products&action=add&id=<?php echo $row['id_number']; ?> "> Add to Cart</a> </td>
    </tr>

    <?php

        } 
    ?>


</table>

1 个答案:

答案 0 :(得分:0)

您的products.php文件包含在第一个文件的中间 已经显示购物车。因此,如果您在products.php中修改$ _SESSION [&#34; cart&#34;],则只有在您下次重新加载页面时才会显示在购物车中。

处理此问题的正确方法是应用MVC pattern或至少使用模板将数据与视图分开。

如果您只是需要快速入侵,请考虑以下代码:

// place this in your first file right after you initialize $page
ob_start();
require($page.".php");
$strPageContents = ob_get_clean();

<?php require($page . ".php"); ?>替换为<?= $strPageContents; ?>