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

201
Visualizações
how to insert a query into my db through a query

I have this function below:

let username = req.session.username;
let timeoflogin = new Date().toLocaleString();
let device = req.headers['user-agent'];
let thequery = query;
let location = 'n/a';

var insertAnalyticsLogin = "insert into ANALYTICS (username, location, device, query, timeoflogin) VALUES ('" + username + "', '" + location + "', '" + device + "', '" + thequery + "', '" + timeoflogin + "')"
console.log(insertAnalyticsLogin);
mysqlconn.connect(function(err) {
    if (err) {
        console.error('Database connection failed: ' + err.stack);
        return;
    }
    
    mysqlconn.query(insertAnalyticsLogin, function(err, rows) {
        if (err) {
            console.log(err);
        } else {
            console.log("successfully inserted into analytics")
            res.redirect('/gen_trainerclientlist')
        }
        mysqlconn.end();
    });
});

and inside thequery, it holds my query from the previous function. It looks something like this:

"insert into USERS 
            (FIRST_NAME, LAST_NAME, USERNAME, USER_ROLE, REGISTER_DATE, 
            PHONE_NUMBER, SUB_STATUS, ASSIGNED_TRAINER) 
    VALUES ('" + clientDetails.inputFirstName + "', '" + 
            clientDetails.inputLastName + "', '" + 
            clientDetails.inputUsername + "','CLIENT','" + 
            currentDate + "', '" + clientDetails.inputPhoneNumber + 
            "','INACTIVE','" + req.session.username + "')"

the error I am getting with this is :

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

by the sounds of it, what is happening is that it is trying to insert thequery again into my database, or it doesn't like the syntax i am providing it with. So i was wondering what the best way to go about this would be. Should i force thequery to be a string, so it forces everything to just get stringified, or am i going about this wrong entierly?

about 4 years ago · Juan Pablo Isaza
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