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

254
Vistas
Why am I getting this MySQL error with node?

I have connected to the database on MySQL, and was able to input data to a table. However, I am not able to input data to another table when it seems that I'm using the exact same method to achieve the input. Below is the code that works:

const mysql = require('mysql');
const connectionPool = mysql.createPool({
    connectionLimit: 1,
    host: "localhost",
    user: "dg",
    password: "123456",
    database: "legends_database",
    debug: false
});
exports.addUser = (Username, Email, Password, response) => {
    //Build query
    let sql = "INSERT INTO user (Username, Email, Password) " +
    "       VALUES ('" + Username + "','" + Email + "'," + Password +")";
    
    //Execute query
    connectionPool.query(sql, (err, result) => {
        if (err){//Check for errors
            let errMsg = "{Error: " + err + "}";
            console.error(errMsg);
            response.status(400).json(errMsg);
        }
        else{//Send back result
            response.send("{result: 'User added successfully'}");
        }
    });
}

I have copied this code and made changes and below is the code that gives me an error.

const mysql = require('mysql');
const connectionPool = mysql.createPool({
    connectionLimit: 1,
    host: "localhost",
    user: "dg",
    password: "123456",
    database: "legends_database",
    debug: false
});
exports.addPost = (Question_Title, Answer_Paragraph, response) => {
    //Build query
    let sql = "INSERT INTO answer (Question_Title, Answer_Paragraph) " +
    "       VALUES ('" + Question_Title + "','" + Answer_Paragraph  +")";
    
    //Execute query
    connectionPool.query(sql, (err, result) => {
        if (err){//Check for errors
            let errMsg = "{Error: " + err + "}";
            console.error(errMsg);
            response.status(400).json(errMsg);
        }
        else{//Send back result
            response.send("{result: 'Post added successfully'}");
        }
    });
}

This is the error I'm getting:

Data received: {"Question_Title":"Test_Question","Answer_Paragraph":"Test_Answer"}
{Error: Error: ER_PARSE_ERROR: 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 ''Test_Answer)' at line 1}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

At first writing queries like this is SQL injection prone, so get rid of this. But if you're using for educational/self-use purposes only - here's my point: I see odd number of single quotes so it seems like the error comes from there(The error message just says that you have a syntax error). So try to write your query like this:

let sql = `INSERT INTO answer (Question_Title, Answer_Paragraph) VALUES ("${Question_Title}","${Answer_Paragraph}");
about 4 years ago · Juan Pablo Isaza Denunciar
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