我把这件事搞定了,但我的 AddItem 函数似乎不起作用。我单击产品页面上的“添加项目”链接,它会将我带到购物车页面。但是购物车页面上没有任何内容。我希望有人能看到它并告诉我该怎么做才能纠正它。
AddItem 函数:
function AddItem($itemId, $qty) {
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead
// Check if this item already exists in the users cart table
$result = mysql_query("select count(*) from cs368_cart where cookieID = '" . GetCartID() . "' and itemId = $itemId");
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0) {
// This item doesn't exist in the users cart,
// we will add it with an insert query
mysql_query("insert into cs368_cart(cookieID, itemId, qty) values('" . GetCartID() . "', $itemId, $qty)");
}
else {
// This item already exists in the users cart,
// we will update it instead
UpdateItem($itemId, $qty);
}
}
我刚刚检查了数据库的 cs368_cart 表,它是空的。
mysql> select * from cs368_cart
-> ;
Empty set (0.00 sec)
所以显然没有添加任何内容。我想知道我的查询是否正确?
我的桌子:
mysql> select * from cs368_products
-> ;
+--------+----------------+---------------------------------------------+-----------+
| itemId | itemName | itemDesc | itemPrice |
+--------+----------------+---------------------------------------------+-----------+
| 1 | French Vanilla | A medium blend with a hint vanilla | 9.79 |
| 2 | Hazelnut Cream | A light blend with a spicy note of Hazelnut | 9.69 |
| 3 | Columbian | A medium-dark blend straight up | 9.89 |
+--------+----------------+---------------------------------------------+-----------+
3 rows in set (0.00 sec)
还有我的购物车桌;
mysql> show columns from cs368_cart;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| cartId | int(11) | NO | PRI | NULL | auto_increment |
| cookieId | varchar(50) | NO | | | |
| itemId | int(11) | YES | | NULL | |
| qty | int(11) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
这是我在一个单独的 php 文件中的 GetCartId,该文件由带有此 AddItem 函数的 php 文件正确调用。
function GetCartId(){
if(isset($_COOKIE["cartId"])){
return $_COOKIE["cartId"];
}
else {
session_start();
setcookie("cartId", session_id(), time()+((3600*24)*30));
return session_id();
}