Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
How to insert an array with some repeating values into mysql using node.js

I'm trying to insert and values into my database and I thought having the same name for the inputs in my form would make it easier for me to insert them in bulk, I want to iterate OrderNo and quantity and insert them to a new column while keeping the other inputs such as fname the same until the values in OrderNo are done.

app.post("/send-data", (req,res)=>{
        let sql = "INSERT INTO foodorder.orders (" +
            "food_id," +
            " qty,"+ 
            " customer_FName," + 
            " customer_LName," +
            " customer_number," +
            " customer_email," +
            " customer_facebook," +
            " order_date," +
            " delivery_option," +
            " mode_of_payment," +
            " delivery_time" +
            ") VALUES (" + 
                ` ${req.body.OrderNo},` +
                ` ${req.body.quantity},` +
                ` "${req.body.fname}",` +
                ` "${req.body.lname}",` +
                ` "${req.body.Contact}",` +
                ` "${req.body.email}",` +
                ` "${req.body.facebook}",` + 
                ` "${req.body.date}",` + 
                ` "${req.body.delivery}",` +
                ` "${req.body.payment}",` +
                `"${req.body.time}"` +
                `)`;      
        con.query(sql, (err,result) => {
            if(!err){
                res.send(result);
            }
            else{
                res.json(req.body);
                throw err;
            }
        })
    });

This is the code that I have and currently it only accepts 1 OrderNo and quantity and when I try to order many products with different quantities it will say column count doesn't match value count at row 1 (see attached photo below)

[the output i get from my forms *I didnt include in the screenshot the other output details such as email etc.][1]

is there a way to just iterate the OrderNo and quantity and insert them creating new columns but with the same fname and lname etc.? or do i need to use MySql cursor?

Note that my code works and inserts if i only have selected 1 order and 1 quantity in my forms

UPDATE

Attached here is what I wanna achieve. I wanna know a way to iterate or loop the OrderNo and quantity and insert them into their respective columns while keeping the other values the same like fname and lname.

I wanna achieve this

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Fixed by iterating the OrderNo. Thanks so much Diogo

app.post("/send-data", (req,res)=>{
        let order = req.body.OrderNo;
        let quantity = req.body.quantity;
        let first = req.body.fname, 
        last = req.body.lname,
        contact = req.body.Contact,
        email = req.body.emailAdd,
        fb = req.body.facebook,
        date = req.body.date,
        delivery = req.body.delivery,
        payment = req.body.payment,
        time = req.body.time,
        address = req.body.address;
        [order].forEach((product, index, arr)=>{
                const q = quantity[index];
                let sql = "INSERT INTO foodorder.orders (" +
                    "food_id," +
                    " qty,"+ 
                    " customer_FName," + 
                    " customer_LName," +
                    " customer_address," +
                    " customer_number," +
                    " customer_email," +
                    " customer_facebook," +
                    " order_date," +
                    " delivery_option," +
                    " mode_of_payment," +
                    " delivery_time" +
                 ") VALUES (" + 
                     con.escape(product) + `,` +
                     con.escape(q) + `,` +
                     con.escape(first) + `,` +
                     con.escape(last) + `,` +
                     con.escape(address) + `,` +
                     con.escape(contact) + `,` +
                     con.escape(""+email) + `,` +
                     con.escape(fb) + `,` +
                     con.escape(date) + `,` +
                     con.escape(delivery) + `,` +
                     con.escape(payment) + `,` +
                     con.escape(time) +
                     `)`; 
                con.query(sql, (err,result) => {
                    if(!err){
                        res.redirect('thankyou.html');
                    }
                    else{
                        res.status(404).send('ERROR. Please Go back and Order Again');
                    }
                })
        })
    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda