Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

78
Views
Express, Mssql insert Into table with variables

I can't for the love of god figure out how to add my Variables into a MSSQL server

I tried everything. Would someone be so kind and pls give me the answer how I can put my Variables into my Database?

I would like to avoid the possibility of a Sql-injection.

app.post('/addUser', addUser)

async function addUser(req, res) {
    let pool;

    const bodylength = req.body.length;
    console.log(bodylength)

    for (let index = 0; index < bodylength; index++) {

        const id = req.body[index].id;
        const first_name = req.body[index].first_name;
        const last_name = req.body[index].last_name;
        const active = switchToBool(req.body[index].active);


        console.log(id, first_name, last_name, active)

        try {

            pool = await sql.connect(config);
            const request = pool.request();

            request.input('id', sql.Int, id)
            request.input('first_name', sql.VarChar(50), first_name);
            request.input('last_name', sql.VarChar(50), last_name);
            request.input('active', sql.Bit, active);

            request.query(`INSERT INTO test (Id, first_name, last_name, active) VALUES (id,first_name,last_name,active)`)

        } catch (error) {
            return res.status(500).send(error)
        }
        res.status(200)
    }
}

I just get a 500 or UnhandledPromiseRejectionWarning: RequestError: Invalid column name 'active'. dependent on what I try.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Normally this should work for what you need to do :

...
request.query("INSERT INTO test (Id, first_name, last_name, active) VALUES ?", [id, first_name, last_name, active])
...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!