1
app.post('', (req, res) => {
    pool.getConnection((err, connection) => {
        if (err) throw err
        console.log('connected as id' + connection.threadID)
        const params = req.body
        var name = params.name
        var tagline = params.tagline
        var description = params.description
        var image = params.image

        connection.query('INSERT INTO beers (name, tagline, description, image) VALUES (name,tagline,description,image)',
            (err, rows) => {
                connection.release()

                if (!err) {
                    res.send('Successfully added record of name' + params.name)
                } else {
                    console.log(err)
                }
            })

        console.log(req.body)
    })
})
1
  • You don't have any placeholders (?) for parameters in your query and you don't add any parameter values to your query. How do you think values (name, tagline ...) will magically pick up your variables? Commented Feb 3, 2022 at 7:36

1 Answer 1

1

As @derpirscher suggested, you should add the parameter values to the query.

Try to change your code like this:

    const { name, tagline, description, image } = req.body.params; 
    connection.query(
      'INSERT INTO beers (name, tagline, description, image) VALUES (?, ?, ?, ?)',
      [name, tagline, description, image],
      (err, rows) => {
        connection.release();

        if (!err) {
          res.send('Successfully added record of name' + params.name);
        } else {
          console.log(err);
        }
      }
    );
Sign up to request clarification or add additional context in comments.

Comments

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.