以最佳标准化将可变数量的数据输入数据库 [英] Entering a variable amount of data into a database with the best normalization possible

查看:85
本文介绍了以最佳标准化将可变数量的数据输入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个包含两个表(产品和供应商)的数据库.

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

所有供应商都填写表格,然后将其数据存储在供应商表中,并且产品表包含所有产品的列表,因此当供应商填写表格时,他可以选择与他一样多的产品希望如此,因为我使用jQuery JSON和AJAX来获取所有产品的列表,然后在其中包含所有产品的下拉列表中填充它们,然后可以根据需要将其克隆多次.

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?

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

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!

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

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) }
        });

}

先谢谢!

推荐答案

如果我从数据库级别正确理解了该问题,则应该使用一个名为ProductSupplier之类的中间表,其中包含一个Product_ID和Supplier_ID列.

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.

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

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.

我的意思是说将供应商和产品ID都添加到此表中的新ROW"

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

这篇关于以最佳标准化将可变数量的数据输入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆