gpt4 book ai didi

php - 以尽可能最佳的规范化方式将可变数量的数据输入数据库

转载 作者:行者123 更新时间:2023-11-29 09:17:22 25 4
gpt4 key购买 nike

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

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

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

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

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

我对关系数据库非常陌生,因此任何有关如何继续的建议都会有很大帮助,你们可能有任何其他建议!

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

$(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 和 Supply_ID 列。

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

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

编辑:我的意思是“将供应商和产品 ID 添加到此表中的新行”

关于php - 以尽可能最佳的规范化方式将可变数量的数据输入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3675711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com