我试图将数组数据插入数据库。有parent_array和child_arrays。
...
titlePanel.setLayout(new GridBagLayout());
titlePanel.setBackground(BLUE);
header = new JLabel ("Gradebook");
header.setLocation(200,400);
header.setFont(new Font("Serif", Font.BOLD, 50));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.CENTER;
titlePanel.add(header,gbc);
date = new Date();
currentDate = new JLabel (fmt.format(date));
currentDate.setFont(new Font("Serif", Font.PLAIN, 14));
ActionListener updateTime = new ActionListener() {
public void actionPerformed (ActionEvent e) {
date = new Date();
currentDate.setText(fmt.format(date));
}
};
Timer timer = new Timer (1000, updateTime);
timer.start();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.CENTER;
titlePanel.add(currentDate, gbc);
JLabel userName = new JLabel ("Username: ");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.EAST;
titlePanel.add(userName, gbc);
JTextField username = new JTextField (10);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;
titlePanel.add(username, gbc);
JLabel password = new JLabel ("Password: ");
gbc.gridx = 0;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.EAST;
titlePanel.add(password, gbc);
JPasswordField Password = new JPasswordField (10);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.WEST;
titlePanel.add(Password, gbc);
JButton login = new JButton ("Login");
gbc.gridx = 0;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.CENTER;
titlePanel.add(login, gbc);
JButton newAccount = new JButton ("Create New Account");
gbc.gridx = 0;
gbc.gridy = 5;
gbc.anchor = GridBagConstraints.CENTER;
titlePanel.add(newAccount, gbc);
mainFrame.add(titlePanel);
HTML:
$arr_parent = ['Apple','Banana','Grape']; //parent_array
$arr_child_par = ['Price','Stock','Date_Stocked']; //first child array ex.
$arr_child_val = ['1000','200','2012-05-05']; //second child array ex.
现在,规则是这样的:
<input type='text' name='arr_parent[]' class='arr_parent' value='"+$('#fruit').val()+"' /> //Each table row has one this textbox
<input type='text' name='arr_child_par[]' class='arr_child_par' value='Price' /> //Each table row has multiple of these textboxes
<input type='text' name='arr_child_val[]' class='arr_child_val' value='1000' /> //Each table row has multiple of these textboxes
长度和值都有所不同,具体取决于$arr_child_par
$arr_parent
长度与$arr_child_val
最后,这是所需输出的表结构:
t_fruit
$arr_child_par
t_fruit_d
NO FRUIT
1 Apple
2 Banana
3 Grape
如何在NO FRUIT PARAMETER PARAMETER_VALUE
1 Apple Price 1000
2 Apple Stock 300
3 Banana Stock 400
4 Banana Supplier John
5 Banana Date_Stocked '2018-02-14'
6 Grape Price 3000
中获取水果并在$arr_parent
上使用它?
答案 0 :(得分:0)
看起来你有3张桌子?果实,参数和参数值。 因此,您应该使用外键,而不是使用参数的“标签” 所以它给出了类似的东西:
Table FRUIT
NO label
1 Apple
2 Banana
3 Grape
Table PARAM
NO label
1 Price
2 Stock
3 Supplier
4 Date_Stocked
etc
Table PARAM_VAL
NO FRUIT_ID PARAM_ID PARAM_VAL
1 1 1 1000
2 1 2 300
3 2 2 400
4 2 3 "John"
使用这种结构,你可以存储它并构建一个对象“fruit_d”但是这个解决方案在PHP中非常难看,并且可能对很多数据非常沉重......所以我建议使用SQL JOIN代替直接在php中构建你的fruit_d
。
答案 1 :(得分:0)
模式
CREATE TABLE fruits (
fruit_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
fruit VARCHAR(100) NOT NULL
);
CREATE TABLE params (
param_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
fruit_id INT NOT NULL,
FOREIGN KEY (fruit_id) REFERENCES fruit (fruit_id)
);
CREATE TABLE params_val (
param_val_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
fruit_id INT NOT NULL,
param_id INT,
FOREIGN KEY (fruit_id) REFERENCES fruits (fruit_id)
FOREIGN KEY (param_id) REFERENCES params (param_id)
);
php更新并加入
<?php
//connect and check
$con = mysqli_connect('localhost', 'root', '', 'mydb');
if (!$con) { exit(); }
// post and serialize
$fruit = implode("'","'", $_POST['arr_parent'] );
$param = implode("'","'", $_POST['arr_child_par'] );
$param_val = implode("'","'", $_POST['arr_child_val'] );
//db insert
$fruit_insert = "INSERT INTO fruits (label) VALUES ('.$fruit.')";
$param_insert = "INSERT INTO param (label) VALUES ('.$param.')";
$param_val_insert = "INSERT INTO param_val (label) VALUES ('.$param_val.')";
//join
$sql = "SELECT fruit, parameter, parameter_val
FROM fruits
LEFT OUTER JOIN params ON fruits.fruit_id = params.fruit_id
INNER JOIN params_val ON params.param_id = params_val.param_id";
$result = mysql_query($sql);
?>
这就是想法......