简体   繁体   English

将多个值插入mysql数据库表

[英]Multiple value insertion into mysql database table

I will use images to explain what i am trying to accomplish. 我将使用图像来说明我要完成的工作。 I am trying to insert Values in a table inputted in a box in any order. 我试图以任何顺序在框中输入的表中插入值。

Lets say the name of your website is your Website.com 假设您的网站名称是您的Website.com

The website lets you input values so that they can be summed up for data analysis like the image below. 该网站允许您输入值,以便可以对它们进行汇总以进行数据分析,如下图所示。

在此处输入图片说明

Your PHP code looking like this:- 您的PHP代码如下所示:-

    <input  type="number"  name="Box1"></input>
    <input  type="number"  name="Box2"></input>
    <input  type="number"  name="Box3"></input>
    <input  type="number"  name="Box4"></input>

Lets further say that your MySQL table in your database looks something like the image below. 再说一遍,数据库中的MySQL表如下图所示。

在此处输入图片说明

The names in the database are 1,2,3...8 as showed above. 如上所述,数据库中的名称为1,2,3 ... 8。

Lets say for Example , the numbers 4,5,6,1 are inputted into the boxes on your site like the image below: 举例来说 ,将数字4,5,6,1输入到您网站上的框中,如下图所示:

在此处输入图片说明

Let also assume that it does not matter how the numbers are entered. 还假设数字的输入无关紧要。 The results should be like the image below in your database table 结果应类似于数据库表中的以下图像

在此处输入图片说明

Lets add more numbers for Example 8,5,2,7 and it does not matter how they are inputted 让我们为示例 8,5,2,7添加更多数字,而无论如何输入 在此处输入图片说明

After the numbers are added, your MySQL database table should look as follows 添加数字后,您的MySQL数据库表应如下所示

在此处输入图片说明

This is what i am trying to achieve. 这就是我想要达到的目标。 Having the table auto increment the entered values could also be nice but i figured it might be harder. 具有表自动递增的输入值也可能很好,但我认为可能会更难。

If the table could auto increment, the the MYSQL table will look something like the image below 如果该表可以自动递增,则MYSQL表将如下图所示

在此处输入图片说明

I need help knowing how to achieve any of the end results. 我需要知道如何实现任何最终结果的帮助。 Any help will be greatly appreciated . 任何帮助将不胜感激 。

Sorry for the long story, i just wanted to explain what i wanted to do as basic as i could. 抱歉,长话短说,我只是想尽可能地解释我想做的事情。

I recommend a different database layout that allows for easier growth of allowed values, easier inserting and easier selection: 我建议使用一种不同的数据库布局,以便更轻松地增加允许的值,更容易插入和更容易选择:

CREATE TABLE results (
  number INT(10) NOT NULL PRIMARY KEY,
  amount INT(10) NOT NULL DEFAULT 1
);

You can easily insert and update the amounts then using the MySQL specific ON DUPLICATE KEY UPDATE : 您可以轻松地插入和更新金额,然后使用特定于MySQL的ON DUPLICATE KEY UPDATE

INSERT INTO results (number)
VALUES (1), (3), (3), (7)
ON DUPLICATE KEY UPDATE amount = amount + 1;

Demo on SQLFiddle: http://sqlfiddle.com/#!2/382cd7/1 关于SQLFiddle的演示: http ://sqlfiddle.com/#!2/382cd7/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM