Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda