【问题标题】:MySQL INSERT query: Unknown column 'undefined' in 'field list'MySQL INSERT 查询:“字段列表”中的未知列“未定义”
【发布时间】:2021-02-05 07:00:04
【问题描述】:

idcost 是整数,不能为 NULL。

我收到此错误:

'错误:ER_BAD_FIELD_ERROR:'字段列表'中的未知列'未定义''

var sql = "INSERT INTO Paintings(`painting_id`,`painting_title`,`img`,`cost`) VALUES("+id+",'"+title+"','"+img+"',"+cost+")";

这是我的html表单

<form action="/post" method="POST">
                    <div class="form-group">
                         <label>Painting id </label>
                         <input type="text" name="painting_id" id="painting_id" >
                    
                    </div>
                    <div class="form-group">
                         <label>Painting Title</label>
                         <input type="text" name="painting_title" id="painting_title" >
                    </div>
                    <div class="form-group">
                         <label>Image </label>
                         <input type="src" name="img" id="img">
                    </div>
                     <div class="form-group">
                         <label>Cost </label>
                         <input type="text" name="cost" id="cost">
                    </div>
                    <br/>
               <input type="submit" value="create painting"/>
</form>
       

这是我的服务器端代码

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
var mysql = require('mysql');
var session = require("express-session");


app.use(express.static(__dirname + '/public'));
app.set("view engine", "ejs");
var urlencodedParser= bodyParser.urlencoded({extended: false});
app.use(express.json());


//======SQL CONNECTION========

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

connection.connect(function(err){
    if(err){
        throw(err);
    }
    console.log("Connected to database");
})


app.get('/admin/painting',function(req,res){
        
        connection.query(`select * from Paintings`,(err,result,fields)=>{
            
            if(err){
                return console.log(err);
            }
            res.render("painting",{page_title:"Customers - Node.js",userData: result});
        });
        
    });

app.get('/painting/add', function(req,res,next){
    res.render('add_painting');

})

app.post('/post', function (req,res,next){

    var id= req.body.painting_id;
    var title = req.body.painting_title;
    var img = req.body.img;
    var cost = req.body.cost;
    var sql = "INSERT INTO Paintings(`painting_id`,`painting_title`,`img`,`cost`) VALUES("+id+",'"+title+"','"+img+"',"+cost+")";
    connection.query(sql,function(err,result){
        if(err) throw(err);
        console.log("Data Inserted");
        res.redirect('/admin/painting');
    });
});

app.listen(1100, ()=> {
    console.log("Server is running");
})

【问题讨论】:

  • id 必须是 undefined,因为这就是错误状态
  • painting_id 是否自动递增列?
  • 不,painting_id INT PRIMARY KEY
  • @JaromandaX 我已将 id 定义为:var id= req.body.painting_id;
  • 在这种情况下,req.body.painting_id 是未定义的

标签: javascript html mysql node.js express


【解决方案1】:

您需要在您的 express 应用中使用 urlencoded 正文解析器

app.use(bodyParser.urlencoded({ extended: false }))

只是声明

var urlencodedParser= bodyParser.urlencoded({extended: false});

实际上并未将其应用于您的应用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2013-01-27
    相关资源
    最近更新 更多