Php 为订单设置数据库

Php 为订单设置数据库,php,database,structure,Php,Database,Structure,我现在正在开发一个iPad订购网络应用程序,它由不同的衣服组成。线条图的构建方式如下所示: CREATE TABLE ordered_items( id INTEGER PRIMARY KEY, order_id INTEGER NOT NULL, product_id INTEGER NOT NULL, quantity INTEGER DEFAULT 0, size TEXT, price NUME

我现在正在开发一个iPad订购网络应用程序,它由不同的衣服组成。线条图的构建方式如下所示:

CREATE TABLE ordered_items(
    id         INTEGER PRIMARY KEY,
    order_id   INTEGER NOT NULL,
    product_id INTEGER NOT NULL,
    quantity   INTEGER DEFAULT 0,
    size       TEXT,
    price      NUMERIC, DEFAULT 0.0,
    updated    DATETIME NOT NULL,
    created    DATETIME DEFAULT current_timestamp
    FOREIGN KEY(order_id) REFERENCES orders(id)
    FOREIGN KEY(product_id) REFERENCES products(id)
)
select * from products where id in (1, 2, 3, 5, 7)
20-30种不同的产品将以相同的形式展示。每种产品都有五种不同的尺码可供选择,每种尺码的数量也不同,这取决于买家想要订购什么

我有一个包含以下列的products表:

id
product_number  
product_name    
product_size    
product_color   
product_description     
product_image   
product_title   
product_category    
product_composition     
product_price_wholesale_dkk     
product_price_wholesale_eur     
product_price_retail_dkk    
product_price_retail_eur    
product_active  
product_detail_one  
product_detail_two  
product_detail_three    
product_xsmall  (activate size for this product? is it avail.)
product_small (activate size for this product? is it avail.)
product_medium (activate size for this product? is it avail.)
product_large (activate size for this product? is it avail.)
product_xlarge (activate size for this product? is it avail.)
id 
order_store 
order_reference 
order_mail 
order_payment_details 
order_products 
order_value_total_wholesale_dkk 
order_value_total_retail_dkk 
order_value_total_wholesale_eur 
order_value_total_retail_eur 
order_date 
order_phone 
order_VAT 
order_address 
order_comments 
order_product_numbers 
order_product_pieces 
order_store_country
以及包含以下列的订单表:

id
product_number  
product_name    
product_size    
product_color   
product_description     
product_image   
product_title   
product_category    
product_composition     
product_price_wholesale_dkk     
product_price_wholesale_eur     
product_price_retail_dkk    
product_price_retail_eur    
product_active  
product_detail_one  
product_detail_two  
product_detail_three    
product_xsmall  (activate size for this product? is it avail.)
product_small (activate size for this product? is it avail.)
product_medium (activate size for this product? is it avail.)
product_large (activate size for this product? is it avail.)
product_xlarge (activate size for this product? is it avail.)
id 
order_store 
order_reference 
order_mail 
order_payment_details 
order_products 
order_value_total_wholesale_dkk 
order_value_total_retail_dkk 
order_value_total_wholesale_eur 
order_value_total_retail_eur 
order_date 
order_phone 
order_VAT 
order_address 
order_comments 
order_product_numbers 
order_product_pieces 
order_store_country
我的问题是我不知道该如何设置数据库。当订单确认书发送给买方时,每种产品都应列出每种产品的具体尺寸数量,并且由于每种尺寸可能被选择,也可能未被选择,因此需要确定选择了哪些尺寸以及特定产品的特定尺寸数量

在我的脚本中选择表时,我如何能够让系统首先检查特定产品的订购尺寸,然后检查该尺寸的多少(数据库方面),然后检查PHP方面


提前感谢。

通过联系的方式来看待这个问题可能会有所帮助。一个订单
有许多
产品,而一个产品
有一个
订单。从那里你可以辨别哪些字段应该是你的外键。如果您决定使用ORM(),它将使与上述关联的交互更容易,但是与ORM相关的学习曲线是存在的

在选择尺寸时,您可能希望保留第三个“查找”表,其中只包含尺寸信息。例如,有一个具有
id
size
的表,其中id是行id,size是“sm”、“med”、“large”等。从那里,有一个product
size
列,您可以将id设置为大小。您可能还希望有一个将已订购的项目与订单关联的表。因此,您将有一个带有
id
(当然),
order\u id
product\u id
count
,并且如果有必要为每个订购的项目存储一个大小,
size
。通过这种方式,您可以存储每个订购项目的大小,并通过以下查询了解订单中哪些项目已设置,哪些项目未设置:
从订购项目中选择产品id,其中大小为“”,订单id为?
。假设MySQL(我相信是SQLite3),该查询将返回订单id为?(无论您的标准是什么),计数是有序的,没有设置大小。您当然希望将ordered_items
size
列设置为可空

很抱歉给出了漫无边际的答案,如果其中任何一个答案不清楚,请让我知道,我会在必要时完善它

更新: 在编辑我的答案时,卢克·皮特曼充实了我试图解释的表格的模型/模式

更新2: 作为对您以下评论的回应:我从未在IOS上使用过sqlite3,但从中可以看出,在设置数据库连接后,您只需直接发出sqlite命令。因此,
ordered_items
的create table命令可以如下所示:

CREATE TABLE ordered_items(
    id         INTEGER PRIMARY KEY,
    order_id   INTEGER NOT NULL,
    product_id INTEGER NOT NULL,
    quantity   INTEGER DEFAULT 0,
    size       TEXT,
    price      NUMERIC, DEFAULT 0.0,
    updated    DATETIME NOT NULL,
    created    DATETIME DEFAULT current_timestamp
    FOREIGN KEY(order_id) REFERENCES orders(id)
    FOREIGN KEY(product_id) REFERENCES products(id)
)
select * from products where id in (1, 2, 3, 5, 7)
或者,如果您的尺寸表具有
id
size
,则可以通过将“尺寸”列更改为:
size\u id INTEGER
并使用以下内容添加FK将另一外键添加到尺寸表中:
外键(size\u id)引用尺寸(id)

注:
您需要阅读更新的第4.3节
和删除的第4.3节
。这些约束有助于保持数据同步,因此,例如,如果您删除了一个产品,但已有订单项引用了该产品id,则可以删除它们或将其设置为某些默认值。根据您的目的,您可能希望也可能不希望这样做,但最好熟悉外键及其可用约束。

我不确定是否完全理解您的问题,但根据我的经验,最好有多个表用于订购。例如,您已经有一个“orders”表-我将添加另一个表“order\u items”,并通过一个关联列(比如“order\u id”)链接它们。因此,理论上,“订单项目”表可以有以下布局:

order_item_id
order_id
product_id
ordered_size
ordered_quantity
ordered_price
...

这有帮助/有意义吗?

嗯,从每种尺寸的产品开始,你不需要在每种产品中存储每种尺寸/颜色/型号。如果您这样设置products_表:

id
product_number  
product_name    
product_size    
product_color   
product_description 
..
product_group
product_amount_available
对于同一件颜色不同的t恤,您将有两个不同的记录(例如)。当买家购买seomthing时,需要选择产品,以及他需要的尺寸和颜色。我刚刚添加了(但不知道您是否需要它)“product\u amount\u available”字段,用于跟踪您拥有的物品的数量(因此您可以说“this product is not available”),以及“product\u group”字段,用于跟踪所有类似的物品(只随大小、颜色或您的想法而变化的物品)

订单表看起来不错

当它到达系统时,它将接收订单,检查id并从products表中检索信息(可能会降低“product\u amount\u available”的值,以反映某些产品已购买),然后创建一个包含按该订单购买的项目的列表,查询如下所示:

CREATE TABLE ordered_items(
    id         INTEGER PRIMARY KEY,
    order_id   INTEGER NOT NULL,
    product_id INTEGER NOT NULL,
    quantity   INTEGER DEFAULT 0,
    size       TEXT,
    price      NUMERIC, DEFAULT 0.0,
    updated    DATETIME NOT NULL,
    created    DATETIME DEFAULT current_timestamp
    FOREIGN KEY(order_id) REFERENCES orders(id)
    FOREIGN KEY(product_id) REFERENCES products(id)
)
select * from products where id in (1, 2, 3, 5, 7)
其中数字是订单中给出的产品的ID

说到用户界面,您可以只显示同一组中的一个,这样用户就可以知道该项上存在变化。您可以使用“select distinct”查询或通过一些PHP编程来实现这一点


我不确定这是你想知道的,希望它能帮助你。你的设计经得起时间的考验。如果你想长期这样做,你一定要看看海伊或西尔弗斯顿的模式书。它们都在狩猎,一个月12次。谢谢你的回复。W