MySQL数据库设置帮助

时间:2010-02-04 20:21:35

标签: php sql mysql database

我正在制作一个分类广告网站...... 我有这6个表: 每个类别都有子类别(或选项),您可以在下面看到。

让我们说用户想要发布一个分类,并将所有信息输入到必要的表单中,我正处于创建PHP代码以实际将数据插入数据库的阶段。

我在想这样的事情:

mysql_query("INSERT INTO classifieds (classified_id, ad_id, poster_id, cat_id, area_id, headline, description) VALUES ($classified_id, '$ad_id', $poster_id, $cat_id, $area_id, '$headline', '$description')");

但我不知道从哪里拿走它...... 我认为海报表不应该是这样的,因为我应该如何确定poster_id应该是什么?或者我应该将其设置为auto-increment? 记住这一点,海报可能无法登录或任何东西,所以如果你知道我的意思,一个人有多个poster_table记录没有问题。

classified_id是PHP生成的随机唯一值,因此始终是唯一的。

请指导我!我不知道如何正确地将表格链接在一起。

如果您有任何问题让我知道,我会更新此问题!

category table:
cat_id (PK)
cat_name

category_options table:
option_id (PK)
cat_id (FK)
option_name

option_values table:
value_id (PK)
option_id (FK)
value

classifieds table:
classified_id (PK)
ad_id (VARCHAR) something like "Bmw330ci_28238239832" which will appear in URL
poster_id (FK)
cat_id (FK)
area_id (FK)
headline
description
price
etc....

posters table:
poster_id (PK)
name 
email
tel
password

area table:
area_id (PK)
area
community

2 个答案:

答案 0 :(得分:1)

你已经有了正确的想法。当有人创建帖子并输入他们的个人信息时,首先将“海报”记录插入海报表中。该表的“poster_id”主键应该是auto_increment字段。

接下来,使用PHP的“mysql_insert_id”获取刚刚创建的新海报的ID。该整数值将是您在“分类”表中的“poster_id”外键字段中输入的数字。

答案 1 :(得分:0)

您通常应将主键设置为自动增量字段。

如果您有链接表并且需要加入id,则可以先插入主表,然后使用函数mysql_insert_id检索刚刚插入的元素的id。然后,您可以使用此值作为外键插入另一个表。

这是一种非常标准的做事方式,所以对你来说应该没问题。