0

I'm trying to get a query to work in node.js but I can't get it to work. I've tested it in MySQL workbench and in phpmyadmin, and the query works in both. Can someone see what the error is?

I've tried adding backticks to the query, didn't work. Tried to use " instead of ` to start the query, didn't work.

app.get('/meetings/add', (req, res) => {
    const { meetingsubject, meetingdescription, bulletpoints, bulletpointslength } = req.query;
    var bulletPointQuery = " INSERT INTO `bulletpoints` (`idmeeting`, `bulletpointname`) VALUES ";
    var allBulletPoints = bulletpoints.split(',');
    for(var i = 0; i < bulletpointslength; i++){
        if(i === bulletpointslength - 1){
            bulletPointQuery = bulletPointQuery  + "(LAST_INSERT_ID(), '" + allBulletPoints[i] + "');";
        }else {
            bulletPointQuery = bulletPointQuery  + "(LAST_INSERT_ID(), '" + allBulletPoints[i] + "'),"
        }
    }
    const insertMeeting = "BEGIN; INSERT INTO `meetings` (`meetingsubject`, `meetingdescription`) VALUES ('" + meetingsubject +"', '" + meetingdescription + "');" + bulletPointQuery + "COMMIT;";

    connection.query(insertMeeting, (err, results) => {
        if(err){
            console.log(err);
            return res.send(err);
        }else{
            console.log("yup");
            res.send("sucessfully added meeting");
        }
    });

});

this is the error:

code: 'ER_PARSE_ERROR', errno: 1064, sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO meetings (meetingsubject, meetingdescription) VALUES ('yayeet'' at line 1", sqlState: '42000', index: 0, sql: "BEGIN; INSERT INTO meetings (meetingsubject, meetingdescription) VALUES ('yayeet', 'yaeter'); INSERT INTO bulletpoints (idmeeting, bulletpointname) VALUES (LAST_INSERT_ID(), 'yeet (07:30)'),(LAST_INSERT_ID(), 'wwee (07:30)');COMMIT;"

EDIT The query i'm trying to work with:

BEGIN;
INSERT INTO `meetings` (`meetingsubject`, `meetingdescription`) 
    VALUES ('yayeet', 'yaeter');
INSERT INTO `bulletpoints` (`idmeeting`, `bulletpointname`)
    VALUES (LAST_INSERT_ID(), 'yeet (07:30)'),
    (LAST_INSERT_ID(), 'wwee (07:30)');
COMMIT;
Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
Zaycn
  • 1
  • don't have an answer, but a suggestion: `const bulletPointQuery = "INSERT INTO \`bulletpoints\` (\`idmeeting\`, \`bulletpointname\`) VALUES (LAST_INSERT_ID(), '" + bulletpoints.split(',').join("'),(LAST_INSERT_ID(), '") + "');";` – Scriptkiddy1337 Aug 29 '19 at 12:38
  • Are you using node-mysql? I don't think it supports doing multiple statements in the same query: https://stackoverflow.com/questions/23266854/node-mysql-multiple-statements-in-one-query – Rob Streeting Aug 29 '19 at 12:57
  • @RobStreeting Thank you very much! Just had to add multipleStatements: true to the connection... – Zaycn Aug 29 '19 at 13:01

0 Answers0