gpt4 book ai didi

mysql - 怎么解决? ER_BAD_FIELD_ERROR : Unknown column 'undefined' in 'field list'

转载 作者:行者123 更新时间:2023-11-29 16:18:28 25 4
gpt4 key购买 nike

我正在尝试使用expressjs将表单数据插入nodejs中的MySQL数据库

当我在命令提示符下运行代码时,它运行良好,但是当我按下提交按钮时,出现以下错误:

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

app.get("/", function(req, res){
res.render("home");
});

//when I press submit button it should post the request and render a page to submit route with text "data saved!!"

app.post("/submit", function(req, res){

var q = "Insert into test (ID, name, crash1, crash2, crash3) VALUES (null, '" + req.body.ANR + "', " + req.body.crash1 + ", " + req.body.crash2 + ", " + req.body.crash3 +")";
connection.query(q, function(err){
if(err) throw err
res.render("home", {message: 'data saved!!'});
})
});

我在 MySQL 命令行中创建了一个表

create table xyz(
ID BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(100) NOT NULL,
crash1 BIGINT,
crash2 BIGINT,
crash3 BIGINT
);

当我手动插入时它起作用了!

insert into xyz(ID, name, crash1, crash2, crash3) VALUES (1,'REERE', 2 ,2 ,2);

我的错误看起来像这样 enter image description here

最佳答案

您正在代码中插入测试表:

var q = "Insert into test (ID, name, crash1, crash2, crash3) VALUES (null, '" + req.body.ANR + "', " + req.body.crash1 + ", " + req.body.crash2 + ", " + req.body.crash3 +")";

但是表名是xyz。您应该用 xyz 替换 test,它应该可以工作。

并且不要在 id 中传递 null 以及 id 不为 null。

请将crash1、crash2、crash3转换为int值:

req.body.crash1 = parseInt(req.body.crash1);
req.body.crash2 = parseInt(req.body.crash2);
req.body.crash3 = parseInt(req.body.crash3);

应该是这样的:

var q = "Insert into xyz (name, crash1, crash2, crash3) VALUES ('" + req.body.ANR + "', " + req.body.crash1 + ", " + req.body.crash2 + ", " + req.body.crash3 +")";

关于mysql - 怎么解决? ER_BAD_FIELD_ERROR : Unknown column 'undefined' in 'field list' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54654619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com