Attaching categories from one table to entries in another MySQL

前端 未结 3 617
攒了一身酷
攒了一身酷 2021-01-23 15:31

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 entr

3条回答
  •  长情又很酷
    2021-01-23 16:00

    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):

    SELECT category_id, category_name
    FROM categories
    

    (... 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

    INSERT INTO tbl_works_categories
        (bus_id, category_id)
    VALUES
        ( $idfromfunction, $categoryidfromform) ;
    

提交回复
热议问题