首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >以尽可能最佳的规范化将可变数量的数据输入到数据库中

以尽可能最佳的规范化将可变数量的数据输入到数据库中
EN

Stack Overflow用户
提问于 2010-09-09 18:26:41
回答 1查看 595关注 0票数 0

好的,我有一个由两个表组成的数据库,products和suppliers。

所有供应商都填写一个表单,然后他们的数据存储在supplier表中,products表包含所有产品的列表,所以当供应商填写表单时,他可以根据需要选择任意多的产品,我使用jQuery JSON和AJAX获取所有产品的列表,然后填充一个包含所有产品的下拉列表,然后可以根据需要多次复制该列表。

我现在遇到的问题是,我如何将供应商选择的所有不同产品插入到供应商表中,或者我应该将他选择的所有产品与一个供应商关联起来,以便更好地标准化,因为所有产品都已经在那里了?

我将使用jQuery $.ajax将表单数据以JSON格式发布到一个等待的PHP文件中,然后文件将对其进行解析并将数据插入到数据库中。

因此,基本上,我需要找出如何将数据库中的数据关联起来,以实现尽可能好的规范化,并且我需要找出一种方法,将可变数量的产品插入到供应商表中,或者找到一种方法,将他选择的许多产品与一个供应商关联起来。

我对关系数据库非常陌生,所以任何关于如何继续的建议都将是一个很大的帮助,所以你们可能有任何其他的建议!

我用来填充、克隆和发布供应商选择的产品的jQuery代码:

代码语言:javascript
代码运行次数:0
运行
复制
$(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) }
        });

}

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-09 18:33:29

如果我从数据库级别正确理解这个问题,您是否应该使用一个类似于ProductSupplier的中间表,其中包含Product_ID和Supplier_ID列。

然后,当供应商选择产品时,将供应商和产品id都添加到此表中的新列中。

这将允许多个供应商挑选相同的产品,并由同一供应商挑选多个产品。

编辑:我的意思是“将供应商和产品id都添加到这个表中的新行”。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3675711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档