# Have someone to know how to insert multiple data to MySQL

**URL:** https://discourse.nodered.org/t/have-someone-to-know-how-to-insert-multiple-data-to-mysql/88337
**Category:** General
**Tags:** function-node
**Created:** [29 May 2024 05:14 UTC](https://discourse.nodered.org/t/have-someone-to-know-how-to-insert-multiple-data-to-mysql/88337 "2024-05-29T05:14:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![usname1121](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/usname1121/32/91443_2.png) [@usname1121](https://discourse.nodered.org/u/usname1121)
#### Post date: [29 May 2024 05:14 UTC](https://discourse.nodered.org/t/have-someone-to-know-how-to-insert-multiple-data-to-mysql/88337/1 "2024-05-29T05:14:39Z")

</div>

I want to insert these data to MySQL database in the one table. how can i insert multiple data in the same time, now i had insert one item data succeed. who can help me?

 ![捕获1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/3/f3bf2859ab64a398a3c268810b882deb8374a0a1.png)  
 ![捕获2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e4e64220eeea7bff4e9757ba4d693a79387baf1.png)

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [29 May 2024 05:39 UTC](https://discourse.nodered.org/t/have-someone-to-know-how-to-insert-multiple-data-to-mysql/88337/2 "2024-05-29T05:39:23Z")

</div>

This is the sort of thing I use. **I've not tried it yet** as I'm not near a computer at the moment.

```auto
// Extract parameters from the incoming message
let value = msg.payload.value;
let tag = msg.payload.tag;

// Get the current time
let currentTime = new Date().toISOString().slice(0, 19).replace('T', ' ');

// Define the prepared statement with placeholders
let query = "INSERT INTO opc_data_to_mysql (value, create_time, tag) VALUES (?, ?, ?)";

// Define the parameters for the placeholders
let params = [value, currentTime, tag];

// Set the query and params on the message
msg.topic = query;
msg.payload = params;

// Return the message to be passed to the MySQL node
return msg;

```

Note 1:  
`new Date().toISOString().slice(0, 19).replace('T', ' ')` creates a timestamp in the format `YYYY-MM-DD HH:MM:SS` which is suitable for MySQL `DATETIME` fields.

Note 2:  
You haven't said if the 'tag' or 'value' fields are a Primary Key.  
If one of them is a Primary Key, then leave it out in the `var params` definition.

Note 3:  
I've assumed all the data is in msg.payload. As @Steve-Mcl says, have a look in the cookbook to see how to 'join' separate messages together.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [29 May 2024 05:52 UTC](https://discourse.nodered.org/t/have-someone-to-know-how-to-insert-multiple-data-to-mysql/88337/3 "2024-05-29T05:52:57Z")

</div>

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [29 May 2024 12:01 UTC](https://discourse.nodered.org/t/have-someone-to-know-how-to-insert-multiple-data-to-mysql/88337/4 "2024-05-29T12:01:22Z")

</div>

You really should look up how to use "prepared statements" as these are by far the most efficient and safest ways to do these kinds of updates.

On top of Dave's notes, please make sure that you SANITISE any inputs:

> **[Exploits of a Mom](https://xkcd.com/327/)**
>
> Her daughter is named Help I'm trapped in a driver's license factory.

[https://bobby-tables.com](https://bobby-tables.com)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [27 August 2024 12:01 UTC](https://discourse.nodered.org/t/have-someone-to-know-how-to-insert-multiple-data-to-mysql/88337/5 "2024-08-27T12:01:26Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
