# \[SOLVED\] Form to mysql error

**URL:** https://discourse.nodered.org/t/solved-form-to-mysql-error/13329
**Category:** General
**Created:** [16 July 2019 04:00 UTC](https://discourse.nodered.org/t/solved-form-to-mysql-error/13329 "2019-07-16T04:00:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![GitHubAndDance](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@GitHubAndDance](https://discourse.nodered.org/u/GitHubAndDance)
#### Post date: [16 July 2019 04:00 UTC](https://discourse.nodered.org/t/solved-form-to-mysql-error/13329/1 "2019-07-16T04:00:32Z")

</div>

Hi,

New to node-red and have been fighting with this for a while now and I seem to be quite stuck...

Using mariaDB with SQL node. Sending data from form to database

The fist part is working fine

This is my whole function block:

```auto
msg.quantity = msg.payload.bt1;
msg.topic = "INSERT INTO `ORDERS` (`OrderID`, `CreatedAt`, `FinishedAt`, `Status`) VALUES (NULL, current_timestamp(), '0000-00-00 00:00:00.000000', '1'); INSERT INTO `QUANTITIES` (`QuantityID`, `ProductType`, `Quantity`, `OrderID`) VALUES (NULL, '1', "+ msg.quantity +",(SELECT OrderID FROM ORDERS ORDER BY OrderID DESC LIMIT 1)); DROP PROCEDURE IF EXISTS dowhile;DELIMITER $$ CREATE PROCEDURE dowhile() BEGIN DECLARE num_rows INT DEFAULT (SELECT Quantity FROM QUANTITIES ORDER BY OrderID DESC LIMIT 1); WHILE num_rows > 0 DO INSERT INTO `BARCODES`(`BarCode`, `OrderID`, `ProductType`) VALUES (NULL, (SELECT OrderID FROM ORDERS ORDER BY OrderID DESC LIMIT 1), '1'); SET num_rows = num_rows - 1; END WHILE; END$$ call dowhile();";
return msg;

```

This is the part giving trouble, but it doesn't give any errors when trying to query it with phpMyadmin and creates the correct rows.:

`DELIMITER $$ CREATE PROCEDURE dowhile() BEGIN DECLARE num_rows INT DEFAULT (SELECT Quantity FROM QUANTITIES ORDER BY OrderID DESC LIMIT 1); WHILE num_rows > 0 DO INSERT INTO`BARCODES`(`BarCode`,`OrderID`,`ProductType`) VALUES (NULL, (SELECT OrderID FROM ORDERS ORDER BY OrderID DESC LIMIT 1), '1'); SET num_rows = num_rows - 1; END WHILE; END$$ call dowhile();"`

 ![nodered-screen1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/5b1aba5b44f11e716ac7d9c7bd100a23da29c04d.jpeg)

---

<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: [16 July 2019 05:59 UTC](https://discourse.nodered.org/t/solved-form-to-mysql-error/13329/2 "2019-07-16T05:59:37Z")

</div>

Why are you dropping & recreating the procedure each time? The purpose of a procedure is to enable the db engine to handle some incoming data more efficiently by compiling the procedure, if you keep recreating it, it can't do that.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [16 July 2019 08:02 UTC](https://discourse.nodered.org/t/solved-form-to-mysql-error/13329/3 "2019-07-16T08:02:42Z")

</div>

Also I suggest splitting it into separate messages, one for each SQL command. Then you will be able more clearly to see whether the problem is the end of one statement or the start of the next.

---

<div class="post-metadata">

### Author: ![GitHubAndDance](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@GitHubAndDance](https://discourse.nodered.org/u/GitHubAndDance)
#### Post date: [16 July 2019 09:15 UTC](https://discourse.nodered.org/t/solved-form-to-mysql-error/13329/4 "2019-07-16T09:15:29Z")

</div>

Thanks for replies.

@TotallyInformation just to make sure the while loop isn't already running when initiating it again. Is that silly?

@Colin how exactly can I do this. As per documentation of node-red-node-mysql the msg.topic should hold the query.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [16 July 2019 09:22 UTC](https://discourse.nodered.org/t/solved-form-to-mysql-error/13329/5 "2019-07-16T09:22:42Z")

</div>

You can send multiple messages from a function node, so something like

```auto
msg1 = {topic: "INSERT INTO `ORDERS` (`OrderID`, `CreatedAt`, `FinishedAt`, `Status`) VALUES (NULL, current_timestamp(), '0000-00-00 00:00:00.000000', '1')"}
msg2 = {topic: "INSERT INTO `QUANTITIES` (`QuantityID`, `ProductType`, `Quantity`, `OrderID`) VALUES (NULL, '1', "+ msg.quantity +",(SELECT OrderID FROM ORDERS ORDER BY OrderID DESC LIMIT 1))"}
msg3 = {topic: "...."};
return( [[msg1, msg2, msg3]])

```

See [https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions)

---

<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: [16 July 2019 10:57 UTC](https://discourse.nodered.org/t/solved-form-to-mysql-error/13329/6 "2019-07-16T10:57:37Z")

</div>

> [@GitHubAndDance](#):
>
> @TotallyInformation just to make sure the while loop isn't already running when initiating it again. Is that silly?

🙂 Yup!

All the stored procedure does is to create a compiled function internal to the db engine that lets you slot data in more efficiently. If you are sending lots of updates of the same structure, procedures are a good bet. But you only create the procedure once - the overheads for creating it are relatively high.

> **[Stored Procedures](https://mariadb.com/kb/en/stored-procedures/)**

Deleting and re-creating it does nothing for you.

---

<div class="post-metadata">

### Author: ![GitHubAndDance](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@GitHubAndDance](https://discourse.nodered.org/u/GitHubAndDance)
#### Post date: [16 July 2019 18:45 UTC](https://discourse.nodered.org/t/solved-form-to-mysql-error/13329/7 "2019-07-16T18:45:00Z")

</div>

Thanks alot @TotallyInformation and @Colin - your help made me fix all of the issues!

I changed topic to [SOLVED]
