PHP MySQL 购物车程序实例_PHP
if(!$session && !$scid) {
$session = md5(uniqid(rand()));
SetCookie("scid", "$session", time() 14400);
} /* last number is expiration time in seconds, 14400 sec = 4 hrs */
class Cart {
function check_item($table, $session, $product) {
$query = "SELECT * FROM $table WHERE session='$session' AND product='$product' ";
$result = mysql_query($query);
if(!$result) {
return 0;
}
$numRows = mysql_num_rows($result);
if($numRows == 0) {
return 0;
} else {
$row = mysql_fetch_object($result);
return $row->quantity;
}
}
function add_item($table, $session, $product, $quantity) {
$qty = $this->check_item($table, $session, $product);
if($qty == 0) {
$query = "INSERT INTO $table (session, product, quantity) VALUES ";
$query .= "('$session', '$product', '$quantity') ";
mysql_query($query);
} else {
$quantity = $qty;
$query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' AND ";
$query .= "product='$product' ";
mysql_query($query);
}
}
function delete_item($table, $session, $product) {
$query = "DELETE FROM $table WHERE session='$session' AND product='$product' ";
mysql_query($query);
}
function modify_quantity($table, $session, $product, $quantity) {
$query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' ";
$query .= "AND product='$product' ";
mysql_query($query);
}
function clear_cart($table, $session) {
$query = "DELETE FROM $table WHERE session='$session' ";
mysql_query($query);
}
function cart_total($table, $session) {
$query = "SELECT * FROM $table WHERE session='$session' ";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_object($result)) {
$query = "SELECT price FROM inventory WHERE product='$row->product' ";
$invResult = mysql_query($query);
$row_price = mysql_fetch_object($invResult);
$total = ($row_price->price * $row->quantity);
}
}
return $total;
}
function display_contents($table, $session) {
$count = 0;
$query = "SELECT * FROM $table WHERE session='$session' ORDER BY id ";
$result = mysql_query($query);
while($row = mysql_fetch_object($result)) {
$query = "SELECT * FROM inventory WHERE product='$row->product' ";
$result_inv = mysql_query($query);
$row_inventory = mysql_fetch_object($result_inv);
$contents["product"][$count] = $row_inventory->product;
$contents["price"][$count] = $row_inventory->price;
$contents["quantity"][$count] = $row->quantity;
$contents["total"][$count] = ($row_inventory->price * $row->quantity);
$contents["description"][$count] = $row_inventory->description;
$count ;
}
$total = $this->cart_total($table, $session);
$contents["final"] = $total;
return $contents;
}
function num_items($table, $session) {
$query = "SELECT * FROM $table WHERE session='$session' ";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
return $num_rows;
}
function quant_items($table, $session) {
$quant = 0;
$query = "SELECT * FROM $table WHERE session='$session' ";
$result = mysql_query($query);
while($row = mysql_fetch_object($result)) {
$quant = $row->quantity;
}
return $quant;
}
}
?>
/*
This part contains a description of how to create the tables on your mysql server.
# MySQL dump 6.0
#
# Host: localhost Database: kmartShopper
#--------------------------------------------------------
# Server version 3.22.25
#
# Table structure for table 'inventory'
#
CREATE TABLE inventory (
product tinytext NOT NULL,
quantity tinytext NOT NULL,
id int(4) DEFAULT '0' NOT NULL auto_increment,
description tinytext NOT NULL,
price float(10,2) DEFAULT '0.00' NOT NULL,
category char(1) DEFAULT '' NOT NULL,
KEY id (id),
PRIMARY KEY (id),
KEY price (price)
);
#
# Table structure for table 'shopping'
#
CREATE TABLE shopping (
session tinytext NOT NULL,
product tinytext NOT NULL,
quantity tinytext NOT NULL,
card tinytext NOT NULL,
id int(4) DEFAULT '0' NOT NULL auto_increment,
KEY id (id),
PRIMARY KEY (id)
);
*/
Example
include("shoppingcart.php");
$cart = new Cart;
$mysql_link = mysql_connect("localhost", "wwwrun", "");
$mysql_select_db("kmartShopper", $mysql_link) /* heh, use whatever database name you put the 2 tables under in place of kmartShopper */
?>
/* call functions like $cart->add_item and such, see the code. */

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

iPhone上的默认地图是Apple专有的地理位置提供商“地图”。尽管地图越来越好,但它在美国以外的地区运行不佳。与谷歌地图相比,它没有什么可提供的。在本文中,我们讨论了使用Google地图成为iPhone上的默认地图的可行性步骤。如何在iPhone中使Google地图成为默认地图将Google地图设置为手机上的默认地图应用程序比您想象的要容易。请按照以下步骤操作–先决条件步骤–您必须在手机上安装Gmail。步骤1–打开AppStore。步骤2–搜索“Gmail”。步骤3–点击Gmail应用旁

您的手机中缺少时钟应用程序吗?日期和时间仍将显示在iPhone的状态栏上。但是,如果没有时钟应用程序,您将无法使用世界时钟、秒表、闹钟等多项功能。因此,修复时钟应用程序的缺失应该是您的待办事项列表的首位。这些解决方案可以帮助您解决此问题。修复1–放置时钟应用程序如果您错误地从主屏幕中删除了时钟应用程序,您可以将时钟应用程序放回原位。步骤1–解锁iPhone并开始向左侧滑动,直到到达“应用程序库”页面。步骤2–接下来,在搜索框中搜索“时钟”。步骤3–当您在搜索结果中看到下方的“时钟”时,请按住它并

C++是一种广泛使用的编程语言,在编写倒计时程序方面非常方便和实用。倒计时程序是一种常见的应用,它能为我们提供非常精确的时间计算和倒计时功能。本文将介绍如何使用C++编写一个简单的倒计时程序。实现倒计时程序的关键就是使用计时器来计算时间的流逝。在C++中,我们可以使用time.h头文件中的函数来实现计时器的功能。下面是一个简单的倒计时程序的代码

session失效通常是由于 session 的生存时间过期或者服务器关闭导致的。其解决办法:1、延长session的生存时间;2、使用持久化存储;3、使用cookie;4、异步更新session;5、使用会话管理中间件。

Java中如何实现一个简单的购物车功能?购物车是在线商店的一个重要功能,它允许用户将想要购买的商品添加到购物车中,并对商品进行管理。在Java中,我们可以通过使用面向对象的方式来实现一个简单的购物车功能。首先,我们需要定义一个商品类。该类包含商品的名称、价格和数量等属性,以及相应的Getter和Setter方法。例如:publicclassProduct

PHPSession跨域问题的解决方法在前后端分离的开发中,跨域请求已成为常态。在处理跨域问题时,我们通常会涉及到session的使用和管理。然而,由于浏览器的同源策略限制,跨域情况下默认情况下无法共享session。为了解决这个问题,我们需要采用一些技巧和方法来实现session的跨域共享。一、使用cookie跨域共享session最常

您在尝试使用应用程序时是否收到“无法允许访问摄像头和麦克风”?通常,您可以在需要提供的基础上向特定对象授予摄像头和麦克风权限。但是,如果您拒绝权限,摄像头和麦克风将无法工作,而是显示此错误消息。解决这个问题是非常基本的,你可以在一两分钟内完成。修复1–提供相机、麦克风权限您可以直接在设置中提供必要的摄像头和麦克风权限。步骤1–转到“设置”选项卡。步骤2–打开“隐私与安全”面板。步骤3–在那里打开“相机”权限。步骤4–在里面,您将找到已请求手机相机权限的应用程序列表。步骤5–打开指定应用的“相机”

如何在MySQL中设计商城的购物车表结构?随着电子商务的快速发展,购物车已成为在线商城的重要组成部分。购物车用于保存用户选购的商品和相关信息,为用户提供方便快捷的购物体验。在MySQL中设计一个合理的购物车表结构,可以帮助开发人员有效存储和管理购物车数据。本文将介绍如何在MySQL中设计商城的购物车表结构,以及提供一些具体的代码示例。首先,购物车表应该包含以
