The console issues an unknown column error 'Tomas' in 'field list'

var name = "Tomas", password = "Cat"; var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "root", password: "password", database: "mydb" }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); var sql = `INSERT INTO customers (name, address) VALUES (${name},${password})`; con.query(sql, function (err, result) { if (err) throw err; console.log("1 record inserted"); }); }); 

The table is empty, created like this:

 var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "root", password: "password", database: "mydb" }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); var sql = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))"; con.query(sql, function (err, result) { if (err) throw err; console.log("Table created"); }); }); 
  • one
    Do you have a column there called "Tomas", and the meaning you insert "Tomas"? Are you sure that is what you want? - Suvitruf
  • Thank you very much, I missed =) - Alexander
  • Unfortunately, this problem did not solve ... - Alexander
  • Show the schema dd. - Suvitruf
  • one
    I see the request to create, I do not see the scheme. Judging by your request, the table column names should be "name" and "address". In this case, this error should not be. Try quoting arguments to take INSERT INTO customers (name, address) VALUES ("${name}","${password})" - Suvitruf

1 answer 1

It is necessary to take a variable in quotes.

 INSERT INTO customers (name, address) VALUES ("${name}","${password})" 

As I understand it, an example from here . There, the string arguments are quoted ((: