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

别来无恙 提交于 2019-12-12 04:27:10

问题


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

All suppliers fill in a form and their data is then stored in the suppliers table, and the products table contains a list of all of the products, so when the supplier fills in the form, he can choose as many products as he wishes as I use jQuery JSON and AJAX to get the list of all of the products and then populate a drop down list with all of them in it, which can then be cloned as many times as is needed.

The problem I am sitting with now is, how do I insert all of the different products the supplier chooses into the supplier table, or should I rather just relate all of the products he chooses to the one supplier for better normalization since all the products are already there?

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

So basically, I need to figure out how to relate the data in the database to achieve the best normalization possible, and I need to figure out a way of inserting a variable amount of products into the suppliers table or find a way to relate the many products he chooses to the one supplier.

I am very new to relational databases, so any advice on how to proceed would be a great help, so would any other advice you guys may have!

The jQuery code I use to populate clone and POST the products the supplier chooses:

$(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's 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!


回答1:


If i understand the problem correctly from a database level, should you be using an intermediate table called something like ProductSupplier containing a Product_ID and Supplier_ID column.

Then when a supplier selects a product, add both the supplier and product id to a new column in this table.

This will allow multiple suppliers to pick the same product and multiple products to be picked by the same supplier.

EDIT: I meant to say "add both the supplier and product id to a new ROW in this table"



来源:https://stackoverflow.com/questions/3675711/entering-a-variable-amount-of-data-into-a-database-with-the-best-normalization-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!