Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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的购物车_Php_Mysql - Fatal编程技术网

使用PHP的购物车

使用PHP的购物车,php,mysql,Php,Mysql,我正在尝试制作购物车。我可以向购物车添加书籍并清空整个购物车。但我无法删除单个购物车项目。我可以向购物车添加一个项目,然后使用“删除项目”超链接将其删除。但添加多个项目后,我无法使用“超链接”删除项目。我该怎么办 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=

我正在尝试制作购物车。我可以向购物车添加书籍并清空整个购物车。但我无法删除单个购物车项目。我可以向购物车添加一个项目,然后使用“删除项目”超链接将其删除。但添加多个项目后,我无法使用“超链接”删除项目。我该怎么办

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> </title>
<link rel="stylesheet" type="text/css" href="header.css" />

</head>

<body>
<div id="header">
<?php include './menu.php';?>
</div>


<div id="navigator">
</div>

<div id="section">

<?PHP
$name=$_SESSION['userName'];
$email=$_SESSION['myEmail'];
require("connection.php");


?><h2>
Welcome <?PHP echo($name); ?>,</h2>
<?PHP require("menu2.php"); ?><hr>

<?PHP 

require("connection.php");
$Query=("select * from tb_book");
$result=mysql_query($Query);
?>

<?php
if(!empty($_GET["action"])) {
switch($_GET["action"]) {

        case "add":
        if(!empty($_POST["quantity"])) {
            $result = mysql_query("SELECT * FROM tb_book WHERE bookID='" . $_GET["bookID"] . "'");
            $productByCode=mysql_fetch_array($result);

            $itemArray = array($productByCode["bookID"]=>array('bName'=>$productByCode["bName"], 'bookID'=>$productByCode["bookID"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["price"]));


            //$itemArray = array($productByCode[0]["bookID"]=>array('bName'=>$productByCode[0]["bName"], 'bookID'=>$productByCode[0]["bookID"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));

            if(!empty($_SESSION["cart_item"])) {
                if(in_array($productByCode["bookID"],$_SESSION["cart_item"])) {
                    foreach($_SESSION["cart_item"] as $k => $v) {
                            if($productByCode["bookID"] == $k)
                                $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
                    }
                } else {
                    $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                }
            } else {
                $_SESSION["cart_item"] = $itemArray;
            }
        }
    break;
    case "remove":
        if(!empty($_SESSION["cart_item"])) {
            foreach($_SESSION["cart_item"] as $k => $v) {
                    if($_GET["bookID"] == $k)
                        unset($_SESSION["cart_item"][$k]);              
                    //if(empty($_SESSION["cart_item"]))
                    //  unset($_SESSION["cart_item"]);
            }
        }
    break;
    case "empty":
        unset($_SESSION["cart_item"]);
    break;  
}
}
?>



<table border="1" width="100%" height="100%">
<tr>


<td width="70%">
<div id="product-grid">
    <div class="txt-heading">Products</div>
    <?php
    $product = mysql_query("SELECT * FROM tb_book ORDER BY bName ASC");
    while($row=mysql_fetch_array($product)) { 

    ?>
        <div class="product-item">
            <form method="post" action="buyBook.php?action=add&bookID=<?php echo $row["bookID"]; ?>">
            <div><img src="./books/<?PHP echo($row['image']); ?>" height="100" width="100" /></div>
            <div><?php echo $row["bName"]; ?></div>
            <div class="product-price"><?php echo "INR &nbsp;".$row["price"]; ?></div>
            <div><input type="text" name="quantity" value="1" size="2" />
            <input type="submit" value="Add to cart" class="btnAddAction" /></div>
            </form>
        </div>
    <?php
            }

    ?>
</div>
</td>

<td width="30%" valign="top">


<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="buyBook.php?action=empty">Empty Cart</a></div>
<?php
if(isset($_SESSION["cart_item"])){
    $item_total = 0;
?>  
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>

<th><strong>Name</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th></th>
</tr>   
<?php       
    foreach ($_SESSION["cart_item"] as $item){
        ?>
                <tr>
                <td><?php echo $item["bName"]; ?></td>

                <td><?php echo $item["quantity"]; ?></td>
                <td align=right><?php echo "INR ".$item["price"]; ?></td>
                <td><a href="buyBook.php?action=remove&bookID=<?php echo $item["bookID"]; ?>" class="btnRemoveAction">Remove Item</a></td>
                </tr>
                <?php
        $item_total += ($item["price"]*$item["quantity"]);
        }
        ?>      
<tr>
<td colspan="5" align=right><strong>Total:</strong> <?php echo "INR ".$item_total; ?></td>
</tr>

</tbody>
</table>        
  <?php
}
?>
</div>
</table>
<br /><br /><center>
    <form name="checkout" action="buyBook_action.php" method="post">
    <input type="submit" value="PROCEED" />
    </form>
    </center>
</td>

</tr>
</table>




</div>


</body>
</html>

欢迎

答复: 在会话变量中,可以使用
bName
bookID
quantity
price
子数组存储数组。我提供的代码将检查
$\u GET[“bookID”]
是否位于
bookID
的子数组中。如果它确实找到了一个,它将删除该数组集

[
  {"bName":"Physics","bookID":"1","quantity":"1","price":"1100.00"},
  {"bName":"Algebra","bookID":"2","quantity":"2","price":"1200.00"},
  {"bName":"Calculus","bookID":"3","quantity":"3","price":"1300.00"}
]
推荐 创建一个额外的表。让我们把它命名为
cart\u table

cart_id  |  userID  |  bookID  |
---------+----------+----------+
   1     |    1     |     1    |
   2     |    1     |     2    |
   3     |    1     |     3    |
userID
列是用户的ID,
bookID
列是用户放入购物车的图书ID

这样做的好处是,即使用户注销,当用户返回时,他/她仍然能够看到他/她放入购物车的书籍。

回答: 在会话变量中,可以使用
bName
bookID
quantity
price
子数组存储数组。我提供的代码将检查
$\u GET[“bookID”]
是否位于
bookID
的子数组中。如果它确实找到了一个,它将删除该数组集

[
  {"bName":"Physics","bookID":"1","quantity":"1","price":"1100.00"},
  {"bName":"Algebra","bookID":"2","quantity":"2","price":"1200.00"},
  {"bName":"Calculus","bookID":"3","quantity":"3","price":"1300.00"}
]
推荐 创建一个额外的表。让我们把它命名为
cart\u table

cart_id  |  userID  |  bookID  |
---------+----------+----------+
   1     |    1     |     1    |
   2     |    1     |     2    |
   3     |    1     |     3    |
userID
列是用户的ID,
bookID
列是用户放入购物车的图书ID


这样做的好处是,即使用户注销,当用户返回时,他/她仍然能够看到他/她放入购物车的书籍。

有错误吗?单击“删除项目”链接后,尝试回显
$\u会话[“购物车项目”]
的内容。只要把这个
var_转储($_SESSION[“cart_item”])在您的“删除”案例中。并查看该会话变量中的ID是否未设置。删除后未取消设置ID。是否有错误?单击“删除项目”链接后,尝试回显
$\u会话[“购物车项目”]
的内容。只要把这个
var_转储($_SESSION[“cart_item”])在您的“删除”案例中。并查看该会话变量中的ID是否未设置。ID在删除后没有取消设置..不…它没有被删除顺便问一下,您的
会话\u start()在哪里?再次尝试在“remove”案例中显示会话数组的内容。session_start()写在menu.php页面中,该页面包含在标题部分。是否检查了“remove Item”链接是否具有正确的对应ID?就像将鼠标指针悬停在该链接上一样,您应该看到它将转到,例如,
buyBook.php?action=remove&bookID=1
。或者检查“Remove Item”链接的元素,如果它有正确的bookID。echo($\u GET[“bookID”])显示的是书籍的正确id,我正在尝试删除它。它没有被删除顺便问一下,您的
会话\u start()
在哪里?再次尝试在“remove”案例中显示会话数组的内容。session_start()写在menu.php页面中,该页面包含在标题部分。是否检查了“remove Item”链接是否具有正确的对应ID?就像将鼠标指针悬停在该链接上一样,您应该看到它将转到,例如,
buyBook.php?action=remove&bookID=1
。或者检查“Remove Item”链接的元素(如果它有正确的bookID)。echo($\u GET[“bookID”])显示的是我试图删除的书籍的正确id