0

I've got some code for testing here:

bot.on('message', (msg) => {
    if (msg.text[3] === '#') {
        bot.sendMessage(msg.chat.id, "Added to DB, thanks!", {});
        connection.query('INSERT INTO botrequests (places_count, discount_count) VALUES (msg.text, msg.text)',
            function (error, results, fields) {
                if (error) throw error;
                console.log("Success");
            });
    }
})

What I wanna do is to insert msg.text twice into a table botrequests in columns places_count and discount_count. Tried using ' ' and " " but this never helped. What's the right way to pass a variable into a query statement?

1 Answer 1

1

In order to pass values to your query, use bind parameters

connection.query('INSERT INTO botrequests (places_count, discount_count) VALUES (?, ?)',
    [msg.text, msg.text],
    function (error, results, fields) {
        if (error) throw error;
        console.log("Success");
    });

See docs

Sign up to request clarification or add additional context in comments.

2 Comments

You made my day!
that's great @lesha_ber ! Remember to set up as a correct answer if it answered your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.