Entering a variable amount of data into the database with the best possible normalization

ok, so I have a database consisting of two tables, products and suppliers.

All suppliers fill out the form, and their data is then stored in the supplier table, and the product table contains a list of all products, so when the supplier fills out the form, he can select as many products as I like using jQuery JSON and AJAX to get a list of all products and then fill in the drop-down list with all of them, which can then be cloned as many times as necessary.

The problem I'm sitting right now is how can I insert all the different products that the supplier selects into the supplier table, or I just have to talk about all the products that he selects for one supplier, for better normalization, since all the products has already?

I will use jQuery $ .ajax to send form data in JSON format to a waiting PHP file, which will then parse it and insert the data into the database.

Basically, I need to figure out how to link the data in the database in order to achieve the best normalization, and I need to figure out a way to insert a variable number of products into the supplier table or find a way to link many products that it chooses for one supplier.

I am very new to relational databases, so any advice on how to proceed next will be helpful, just like any other advice that you guys may have!

The jQuery code that I use to populate the clones and POST products that the provider selects:

$(document).ready(function() {

        var count = 0;      

        //when clicked it will remove the closest div with a class of 'container'
        $("span.remove").live('click', function(){
            $(this).closest("div.container").fadeOut(400, function(){
                $(this).remove();
                $('#button').attr('disabled','');
            });
        });

        //initialize the button
        $('#button').attr('disabled','');
        $('#button').click(function(){

            var count = $("#systems_wrapper > .container").size();
            var lastID = $("#systems_wrapper > .container:last").attr('id');
            var exploded = lastID.split("_");
            var increment = Number(exploded[1])+1;

            //if the user has selected 5 products, disable the 'add' button
            if(count >= 5){
                $('#button').attr('disabled','disabled');
            }else {
                $('#button').attr('disabled','');
            }

            //clone the first drop down and give it a different ID, as well as it child elements
            var test = $('#systems_0.container').clone().attr('id', 'system_' + increment).appendTo('#systems_wrapper');
            test.children(':nth-child(2)').append('<span class="remove"></span>');
            test.children(':nth-child(2)').children(':first').attr('id', 'mail_' + increment).attr('class','dropDowns').attr('onchange','test();');


            });


    //get the products JSON object returned from test_post.php and run the necessary functions on the returned data
    $.getJSON("test_post.php", function(data){

    //clean out the select list
    $('#box').html('');

        //run the loop to populate the drop down list
        $.each(data, function(i, products) {
            $('#box').append(
                $('<option></option>').html(products.products)
            );
        });
    });
});


//this gets all of the products chosen and then gets each ones value and ID, and then posts it to the qwer.php file

function test(){
    var sections = $('#systems_wrapper').find('.dropDowns');
    var newArray = new Array();

    sections.each(function(){
        var id = $(this).attr('id');
        var val = $(this).val();
        var o = { 'id': id, 'value': val };

        newArray.push(o);
    });

    alert(newArray);

    $.ajax({
            type: "POST",
            url: "qwer.php",
            dataType: 'json',
            data: { json: JSON.stringify(newArray) }
        });

}

Thanx in advance!

0
source share
1 answer

If I understand the problem correctly from the database level, should I use a staging table called ProductSupplier containing the column Product_ID and Supplier_ID.

Then, when a vendor selects a product, add the vendor and product identifier to a new column in this table.

, .

EDIT: : " ROW "

+2

Source: https://habr.com/ru/post/1764287/


All Articles