将一个表中的类别附加到另一个 MySQL 中的条目
我有一个数据库,它接受用户提交的数据,我想将这些条目分组到大约 10 个类别中的一个或多个类别中。
例如,您将您的条目添加到我的网站,说这一切都与您的业务(汽车代客服务)有关,我为您提供机会将您的条目分类为任意 10 个固定类别(汽车、移动服务等) ,因此,如果用户在“汽车”或“移动服务”类别下搜索企业,则会从查询中返回您的企业。
因此,正如我从这里获取的大部分答案一样,为了实现这一目标,我的数据库包含三个表(结构如下),一个用于您的业务条目,一个列出设置的类别,以及一个我添加到的关系表前两个表中的唯一键。
CREATE TABLE `business` (
`bus_id` INT NOT NULL AUTO_INCREMENT,
`bus_name` VARCHAR(50) NOT NULL,
`bus_dscpn` TEXT NOT NULL,
`bus_url` VARCHAR(255) NOT NULL,
PRIMARY KEY (`bus_id`)
)
CREATE TABLE `categories` (
`category_id` INT NOT NULL AUTO_INCREMENT,
`category_name` VARCHAR(20) NOT NULL,
PRIMARY KEY (`category_id`)
)
CREATE TABLE `tbl_works_categories` (
`bus_id` INT NOT NULL,
`category_id` INT NOT NULL
)
我一生都无法弄清楚的是,当您从我用 PHP 处理的表单中选择您希望与您的业务关联的类别时,如何在数据库中实际关联它们!
I have a database which takes user submitted data, the entries from which I want to group under one or several of about 10 categories.
So for example, you add your entry to my site, say its all about your business (a car valeting service), and I offer you the opportunity to categorize your entry in any number of 10 fixed categories (automotive, mobile service, etc), so if a user searches for businesses under the 'automotive' or 'mobile service' category, your business is returned from the query.
So as I have taken from most of the answers on here, to achieve this I have my database with three tables (structure below), one for your business entry, one listing the set categories, and one relational table to which I've added the unique key from the prior two tables.
CREATE TABLE `business` (
`bus_id` INT NOT NULL AUTO_INCREMENT,
`bus_name` VARCHAR(50) NOT NULL,
`bus_dscpn` TEXT NOT NULL,
`bus_url` VARCHAR(255) NOT NULL,
PRIMARY KEY (`bus_id`)
)
CREATE TABLE `categories` (
`category_id` INT NOT NULL AUTO_INCREMENT,
`category_name` VARCHAR(20) NOT NULL,
PRIMARY KEY (`category_id`)
)
CREATE TABLE `tbl_works_categories` (
`bus_id` INT NOT NULL,
`category_id` INT NOT NULL
)
What I cannot figure out for the life of me is, when you select which categories you'd like your business associated with from my form which is processed with PHP, how to actually associate them in the database!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要获取包含您可以执行的所有类别的业务数据,
要从类别名称检索业务数据,只需切换 WHERE 参数
To get a business data with all categories you can do,
To retrieve business data from category name, just switch the WHERE param
我假设您的表单返回每个所选类别的类别 ID,并且这些 ID 与类别表中的category_id 相对应。
首先,将业务记录插入到业务表中,这将为您提供自动递增编号(PHP 中的 mysql_insert_id 函数或您正在使用的库的任何函数)。因此,您在某处存储了一个
$idfromfunction
。使用该企业 ID,然后循环遍历您的类别 ID
(可能已经使用过):
(...并通过复选框或下拉列表或其他方式向您的用户显示此列表)
在用户选择一个
$categoryidfromform
,然后您可以插入到 tbl_works_categories 表中I assume your form returns the category IDs for each category selected and that those IDs correspond with category_id in your categories table.
First you insert your business record into your business table, this will give you the auto increment number (mysql_insert_id function in PHP or whatever function for the library you are using). So, you have an
$idfromfunction
somewhere stored.With that business ID, then loop through your category IDs
(perhaps already having used):
(... and showed this list to your user through checkboxes or drop list or whatever)
After the user chooses one
$categoryidfromform
, you can then insert into your tbl_works_categories table我同意你的观点,这很简单。也许这会回答你的问题。
我假设您的表单具有复选框或多选框,以允许用户为其业务选择类别。并且复选框/选择框的“value”属性设置为存储在
categories
表中的类别 ID。因此,现在提交表单后,您将进入 PHP 脚本以及表单中的其他输入,其中包含用户选择的类别 ID 的数组。我们将其称为 $_POST['categories']1:
方法 运行查询 - INSERT INTO
business
...使用
mysql_insert_id
或mysqli->insert_id
读取business
表中最后一个条目的ID,具体取决于您使用的是MySQL还是MySQLi 库。假设您将此 ID 存储在$busId
c 中。使用
implode()
从 $_POST['categories'] 生成逗号分隔的字符串。假设您将此字符串存储在$strCategories
d 中。
方法2:
或者,您也可以循环遍历POST数组并生成INSERT查询,例如:
希望以上有所帮助。我尚未测试上述任何代码,因此如果有任何问题,您可能需要进行一些调试。如果您有任何疑问,请随时询问。
I agree with you that it's simple. Perhaps this will answer your questions.
I assume that your form has check-boxes or a multi-select box to allow the user to select categories for his/her business. And that the "value" attribute of the check-box/select-box is set to the category ID, as stored in the
categories
table. So, now when the form is submitted, you'll get in the PHP script, along with other inputs from the form, an array with the IDs of the categories selected by the user. Let's call it $_POST['categories']METHOD 1:
a. Run the query - INSERT INTO
business
...b. read the ID of the last entry made into
business
table using eithermysql_insert_id
ormysqli->insert_id
, depending on either you are using MySQL or MySQLi library. Suppose you store this ID in$busId
c. From $_POST['categories'], generate a comma-separated string using
implode()
. Suppose you store this string in$strCategories
d.
METHOD 2:
Alternatively, you may as well loop through the POST array and generate INSERT query, like:
Hope the above helps. I haven't tested any of the above codes, so you might need to do some debugging if anything is broken. Please feel free to ask if you've any questions.