donglaogu3788 2015-11-04 10:44
浏览 110

将购物车会话保存到数据库

I have a ordering page where i am letting the user to order and which is being added to the shopping cart. And then in the cart page i am showing the orders and the user can change their orders. What i want to do is to save this cart information into database so that i can implement a checkout which will also insert the cart contents to the database. I have looked online but not finding any solution. Below is my code - index.php

    <?php 
    session_start(); 
    require("connection.php"); 
    if(isset($_GET['page'])){ 

        $pages=array("products", "cart"); 

        if(in_array($_GET['page'], $pages)) { 

            $_page=$_GET['page']; 

        }else{ 

            $_page="products"; 

        } 

    }else{ 

        $_page="products"; 

    } 


    ?> 


<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>



    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <!-- <link rel="stylesheet" href="css/reset.css" />  -->
    <link rel="stylesheet" href="styles.css" /> 


    <title>HackLunch</title> 


</head> 

<body> 

    <h1> Welcome to our site </h1>

    <div id="container"> 

        <div id="main"> 

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

        </div><!--end of main--> 

        <div id="sidebar"> 
        <h1>Cart</h1> 
<?php 

    if(isset($_SESSION['cart'])){ 

        $sql="SELECT * FROM products WHERE id_product IN ("; 

        foreach($_SESSION['cart'] as $id => $value) { 
            $sql.=$id.","; 
        } 

        $sql=substr($sql, 0, -1).") ORDER BY name ASC"; 
        $query=mysql_query($sql); 
        while($row=mysql_fetch_array($query)){ 

        ?> 
            <p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p> 
        <?php 

        } 
    ?> 
        <hr /> 
        <a href="index.php?page=cart">Go to cart</a> 
    <?php 

    }else{ 

        echo "<p>Your Cart is empty. Please add some products.</p>"; 

    } 

?>

        </div><!--end of sidebar--> 

    </div><!--end container--> 

</body> 
</html>

cart.php

    <?php 

    if(isset($_POST['submit'])){ 

        foreach($_POST['quantity'] as $key => $val) { 
            if($val==0) { 
                unset($_SESSION['cart'][$key]); 
            }else{ 
                $_SESSION['cart'][$key]['quantity']=$val; 
            } 
        } 

    } 

?> 

<h1>View cart</h1> 
<a href="index.php?page=products">Go back to the products page.</a> 
<form method="post" action="index.php?page=cart"> 

    <table> 

        <tr> 
            <th>Name</th> 
            <th>Quantity</th> 
            <th>Price</th> 
            <th>Items Price</th> 
        </tr> 

        <?php 

            $sql="SELECT * FROM products WHERE id_product IN ("; 

                    foreach($_SESSION['cart'] as $id => $value) { 
                        $sql.=$id.","; 
                    } 

                    $sql=substr($sql, 0, -1).") ORDER BY name ASC"; 
                    $query=mysql_query($sql); 
                    $totalprice=0; 
                    while($row=mysql_fetch_array($query)){ 
                        $subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price']; 
                        $totalprice+=$subtotal; 
                    ?> 
                        <tr> 
                            <td><?php echo $row['name'] ?></td> 
                            <td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>" /></td> 
                            <td><?php echo $row['price'] ?>$</td> 
                            <td><?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?>$</td> 
                        </tr> 
                    <?php 

                    } 
        ?> 
                    <tr> 
                        <td colspan="4">Total Price: <?php echo $totalprice ?></td> 
                    </tr> 

    </table> 
    <br /> 
    <button type="submit" name="submit">Update Cart</button> 
    <a href="checkout.php">Checkout</a>
</form> 
<br /> 
<p>To remove an item set its quantity to 0. </p>

products.php

    <?php 

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

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

        if(isset($_SESSION['cart'][$id])){ 

            $_SESSION['cart'][$id]['quantity']++; 

        }else{ 

            $sql_s="SELECT * FROM products 
                WHERE id_product={$id}"; 
            $query_s=mysql_query($sql_s); 
            if(mysql_num_rows($query_s)!=0){ 
                $row_s=mysql_fetch_array($query_s); 

                $_SESSION['cart'][$row_s['id_product']]=array( 
                        "quantity" => 1, 
                        "price" => $row_s['price'] 
                    ); 


            }else{ 

                $message="This product id it's invalid!"; 

            } 

        } 

    } 

?> 
    <h1>Product List</h1> 
    <?php 
        if(isset($message)){ 
            echo "<h2>$message</h2>"; 
        } 
    ?> 
    <table> 
        <tr> 
            <th>Name</th> 
            <th>Price</th> 
            <th>Action</th> 
        </tr> 

        <?php 

            $sql="SELECT * FROM products ORDER BY name ASC"; 
            $query=mysql_query($sql); 

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

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

            } 

        ?> 

    </table>

I have tried to implement my checkout.php like this but it does not work.

    <?php
include("connection.php");
$serialized_cart = serialize($_SESSION["cart"]); 
$sql = "INSERT INTO cart (contents) VALUES ('" . mysql_real_escape_string($serialized_cart) . "')"; 
mysql_query($sql) 
  or die("Query to store cart failed"); 
?>
  • 写回答

1条回答 默认 最新

  • du13932014807 2015-11-04 10:54
    关注

    You will probably be better of if you insert ROWS rather than SERIALIZED DATA. (It's kind of ugly, you know..)

    Iterate over your rows as you already do:

    $sql="SELECT * FROM products WHERE id_product IN ("; 
                foreach($_SESSION['cart'] as $id => $value) { 
                    $sql.=$id.","; 
     //execute an insert here..
                } 
    

    And spend some time learning php PDO. You will probably not regret it and receive less bashing.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?