简体   繁体   English

SQl 不接受节点 js 中的插入查询抛出 ER_BAD_FIELD_ERROR

[英]SQl not accepting insert query in node js throws ER_BAD_FIELD_ERROR

const axios = require('axios');
const mysql = require('mysql');

var con = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'YEX8QHOG@a',
    database: 'test',
});

con.connect((err) => {
    if (err) {
        throw err;
    }
    console.log('Connected...');
});

let createTable = 'CREATE TABLE employee(id VARCHAR(5),empname VARCHAR(30),empsal VARCHAR(30),empage VARCHAR(30))';
con.query(createTable, function (err, result, feilds) {
    if (err) {
        throw err;
    }
    console.log('Db Created');
    console.log(result);
});

function setData(id, empname, empsal, empage) {
    let query = `INSERT INTO employee (id,empname,empsal,empage) VALUES (${id},${empname},${empsal},${empage})`;
    con.query(query, function (err, result) {
        if (err) {
            throw err;
        } else {
            console.log('Data insert', id);
            console.log(result);
        }
    });
}

async function putData() {
    let url = 'http://dummy.restapiexample.com/api/v1/employees';
    let data = await axios.get(url);
    let dataarr = await data.data.data;
    console.log(dataarr);

    setData('1', 'Karan', '36000', '20');
    dataarr.map((emp) => {
    setData(emp.id, emp.employee_name, emp.employee_salary, emp.employee_age);
        console.log(typeof emp.id, typeof emp.employee_name, typeof emp.employee_salary, typeof emp.employee_age);
    });
}

putData();

code: 'ER_BAD_FIELD_ERROR', errno: 1054, sqlMessage: "Unknown column 'Karan' in 'field list'", sqlState: '42S22', index: 0, sql: 'INSERT INTO employee (id,empname,empsal,empage) VALUES (1,Karan,36000,20)'代码:'ER_BAD_FIELD_ERROR',errno:1054,sqlMessage:“'字段列表'中的未知列'Karan'”,sqlState:'42S22',索引:0,sql:'INSERT INTO员工(id,empname,empsal,empage)值 (1,Karan,36000,20)'

when I pass values they throw this error for employee name当我传递值时,他们会为员工姓名抛出此错误

I'd suggest using parameters, this has other benefits too (especially the prevention of SQL injection),我建议使用参数,这也有其他好处(特别是防止 SQL 注入),

This is documented at: mysql#escaping-query这记录在: mysql#escaping-query

You can change the function setData easily to do this:您可以轻松更改 function setData 来执行此操作:

function setData(id, empname, empsal, empage) {
    let query = `INSERT INTO employee (id,empname,empsal,empage) VALUES (?,?,?,?)`;
    let parameters = [id, empname, empsal, empage];
    con.query(query, parameters, function (err, result) {
        if (err) {
            throw err;
        } else {
            console.log('Data insert', id);
            console.log(result);
        }
    });
}
let query = `INSERT INTO employee (id,empname,empsal,empage) VALUES ('${id}','${empname}','${empsal}','${empage}')`;

use quotes for any kind of data '${id}'对任何类型的数据 '${id}' 使用引号

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 node.js mysql 查询 product_id 响应 "code": "ER_BAD_FIELD_ERROR" "errno": 1054 - node.js mysql query product_id response "code": "ER_BAD_FIELD_ERROR" "errno": 1054 错误:ER_BAD_FIELD_ERROR:节点js中“字段列表”中的未知列“asd123” - Error: ER_BAD_FIELD_ERROR: Unknown column 'asd123' in 'field list' in node js ER_BAD_FIELD_ERROR,即使查询正确 - ER_BAD_FIELD_ERROR, even though query is correct NodeJ和MySQL ER_BAD_FIELD_ERROR - NodeJs and MySQL ER_BAD_FIELD_ERROR Sails.js,Model.update()抛出ER_BAD_FIELD_ERROR:'where子句'中的未知列'undefined' - Sails.js, Model.update() throws ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'where clause' SQL 查询错误:错误:ER_BAD_FIELD_ERROR:“有子句”中的未知列“Enrollment.final_grade” - SQL Query Error: Error: ER_BAD_FIELD_ERROR: Unknown column 'Enrollment.final_grade' in 'having clause' MySQL 插入错误:ER_BAD_FIELD_ERROR:“字段列表”中的未知列“2525” - MySQL insert error : ER_BAD_FIELD_ERROR: Unknown column '2525' in 'field list' ER_BAD_FIELD_ERROR:“字段列表”中的未知列 - ER_BAD_FIELD_ERROR: Unknown column in 'field list' 具有环回的findOne上的Mysql ER_BAD_FIELD_ERROR - Mysql ER_BAD_FIELD_ERROR on findOne with loopback 为什么我的 sequelize 查询在急切加载与另一个关联的模型时会因 ER_BAD_FIELD_ERROR 而中断? - Why does my sequelize query break with ER_BAD_FIELD_ERROR when eager loading a model associated to another with belongsTo/hasMany?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM