How to create product relationship with php and MySQL

1

Some time ago I made a product relationship attempt, but it was not functional and I think it was not the right way, I confess, the same is bad, what I did was basically this:

I look for the lines (Categories) and just below where the checks are ready the products of this category and select the product that I want to relate, this can be seen in this image:

Mycheksareconfiguredlikethis:

<inputtype="checkbox" name="produto[]" id="produto[]" value="<?php echo $row_produtos['id_produto']; ?>" /><?php echo $row_produtos['codigo']; ?>

I'm limiting the amount of 10 products, I'm confused and I can not understand what I should write to my bank, product_id, category_id to later fetch and display on my page.

What I would like to know is, how is the correct way to develop this solution in the visual, what would the registration look like?

    
asked by anonymous 17.03.2017 / 19:19

1 answer

1

One suggestion:

First, if I'm not mistaken, you should not make an HTML ID with array like this: id="product []" (leave only the name)

Now: In the database part, you would simply create a table intermediate table to relate the products to each other, or, you would have two foreign keys in that table pointing to the same primary key as your products.

In the visual part. You could categorize this sort of thing into editing the product itself, rather than its category.

On the product screen, you can list all other products in the same category and thus, you select which ones are related. This would be saved with N (where N can be up to 10 by your rule) lines in that new table where the first key is the product itself, and the second key is the related product.

Just remember that you would always have to search the two keys, since the product itself may be in the first and / or second.

    
20.03.2017 / 13:51