我正在創建一個電子商務商店,並在SQL數據庫中設置了購物車。我設法讓購物車工作,並在數據庫中設置了一些表格。我現在在使用不同尺寸的產品時遇到問題,例如襯衫將有3種不同的尺寸,每種尺碼也會有不同的價格。SQL從多個表中選擇數據
這裏是我的表是如何設置:
CREATE TABLE products(
id int(11) NOT NULL auto_increment,
product_name varchar(255) NOT NULL,
price varchar(16) NOT NULL,
details text NOT NULL,
category varchar(16) NOT NULL,
subcategory varchar(16) NOT NULL,
date_added date NOT NULL,
PRIMARY KEY(id),
UNIQUE KEY product_name (product_name)
)
CREATE TABLE admin(
id int(11) NOT NULL auto_increment,
username varchar(24) NOT NULL,
password varchar(24) NOT NULL,
last_log_date date NOT NULL,
PRIMARY KEY(id),
UNIQUE KEY username (username)
)
CREATE TABLE sizes(
SizeID int(11) NOT NULL auto_increment,
id int(11) NOT NULL auto_increment,
Size varchar(24) NOT NULL,
Price varchar(6) NOT NULL,
PRIMARY KEY(sizeID),
UNIQUE KEY ID (ID)
)
我可以抓住從產品表,名稱,價格,類別等信息,但我會怎麼選擇尺寸表中的信息,以及產品表。這些信息必須匹配,所以表格必須由FK鏈接?
if(isset($_GET['id'])){
$id = preg_replace('#[^0-9]#i', '', $_GET["id"]);
//use this var to check to see if this ID exists, if yes then get the product
//details, if no then ecit this script and give message why
$sql=mysql_query("SELECT*FROM products WHERE id='$id' LIMIT 1");
$productCount=mysql_num_rows($sql);//count number of rows in sql variable
if($productCount>0){
//get all product details
while($row=mysql_fetch_array($sql)){
$id=$row["id"];
$product_name=$row["product_name"];
$price=$row["price"];
$details=$row["details"];
$category=$row["category"];
$subcategory=$row["subcategory"];
$date_added = strftime("%b %d %Y",strtotime($row["date_added"]));
//product_name, price, details, category, subcategory, date_added
}
}else{
echo "This item does not exist.";
exit();
}
}else{
echo "Data to render this page is missing.";
exit();
}
這將從產品表中獲取數據,但我仍然需要尺寸表中的尺寸和價格,並且他們需要匹配物品ID。我將如何去做這件事?
答案是「這個信息必須匹配,所以表必須由FK鏈接?」,是的。 – 2013-04-30 21:45:54
我會建議查看PDO(http://php.net/manual/en/book.pdo.php)並準備特別爲做電子商務網站的報表。 – ewein 2013-04-30 21:56:33
在一個表中不能有2個自動增量列。 – bksi 2013-04-30 22:18:09