将可变数量的数据输入到具有最佳标准化的数据库中

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

所有供应商填写表格,然后将他们的数据存储在供应商表格中,产品表格中包含所有产品的清单,因此当供应商填写表格时,他可以选择尽可能多的产品。使用jQuery JSON和AJAX获取所有产品的列表,然后填充一个包含所有产品的下拉列表,然后可以根据需要克隆多次。

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

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

所以基本上,我需要弄清楚如何关联数据库中的数据以实现最佳的标准化,我需要找出一种方法将可变数量的产品插入供应商表或找到一种方法来关联许多他选择的产品给一个供应商。

我对关系数据库很新,所以关于如何进行的任何建议都会有很大帮助,所以你们可能会有任何其他建议!

我用jQuery代码填充克隆和POST供应商选择的产品:

$(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(''); 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( $('').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提前!

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

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

这将允许多个供应商选择相同的产品和同一供应商挑选的多个产品。

编辑:我想说“将此供应商和产品ID添加到此表中的新ROW”