无法在 mysql 和 Node JS 中实现 CRUD 操作
Unable to implement CRUD operation in mysql and Node JS
我正在尝试为我的 Node Js 应用程序实现 crud 操作。我正在按数据库使用 mysql。
下面是代码。
你们能不能请大家回顾一下,让我知道我哪里出错了。
下面是我的 Jade 模板
table#myTable.table.table-striped.table-bordered.table-sm.table-hover
thead(style='background-color:indigo ; color:white; text-align: center;')
tr
th.th-sm No
th.th-sm Product
th.th-sm Owner Name
th.th-sm Description
th.th-sm Last update
th.th-sm Auditor
th.th-sm Audit Date
th.th-sm Any Action
th.th-sm Result
th.th-sm Edit
th.th-sm Delete
tbody
each item in items ? items : []
tr
td=item.id
td= item.product
td= item.owner
td= item.description
td= item.date
td= item.Auditor
td= item.adate
td= item.Action
td= item.result
td
a(href='/Edit/{item.id}') Update
td
a(href='/delete/{item.id}') Delete
这里是节点js代码
const express = require('express')
var mysql = require('mysql')
const bodyParser = require('body-parser')
const app = express()
var flash = require('connect-flash')
var session = require('express-session');
//...
app.use(session({ cookie: { maxAge: 60000 },
secret: 'woot',
resave: false,
saveUninitialized: false}));
const port = 3000
app.use('/public', express.static(__dirname + '/public'));
app.use('/Dashboard', express.static(__dirname + '/views'));
app.use(flash());
app.use(bodyParser.urlencoded({extended: false}))
app.set('view engine', 'pug')
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'audit',
timezone: 'Z'
})
connection.connect(function(err){
if (err) throw err;
connection.query("SELECT * FROM auditresult", function (err, result) {
if (err) throw err;
console.log('Data has been Exported successfully'+ result.length);
});
})
// DELETE
app.get('/delete/:id', function(req, res, next) {
connection.query("DELETE FROM auditresult WHERE id =' " +req.params.id+" ' ", function(err, result) {
res.redirect('Report')
})
})
// Edit
app.get('/Edit/:id', function(req, res, next){
var id= req.params.id;
var sql="SELECT * from auditresult where id= " +req.params.id+" ;"
connection.query(sql, function(err,items,fields){
res.render('Update',{ product: items[0]});
})
});
当我点击“编辑”时,我得到的是 URL“http://127.0.0.1:3000/Edit/%7Bitem.id%7D”
预先感谢您的帮助
我认为您需要在 jade 模板中更正此问题:
a(href='/Edit/' + item.id) 和 a(href='/delete/' + item.id)
我正在尝试为我的 Node Js 应用程序实现 crud 操作。我正在按数据库使用 mysql。 下面是代码。 你们能不能请大家回顾一下,让我知道我哪里出错了。 下面是我的 Jade 模板
table#myTable.table.table-striped.table-bordered.table-sm.table-hover
thead(style='background-color:indigo ; color:white; text-align: center;')
tr
th.th-sm No
th.th-sm Product
th.th-sm Owner Name
th.th-sm Description
th.th-sm Last update
th.th-sm Auditor
th.th-sm Audit Date
th.th-sm Any Action
th.th-sm Result
th.th-sm Edit
th.th-sm Delete
tbody
each item in items ? items : []
tr
td=item.id
td= item.product
td= item.owner
td= item.description
td= item.date
td= item.Auditor
td= item.adate
td= item.Action
td= item.result
td
a(href='/Edit/{item.id}') Update
td
a(href='/delete/{item.id}') Delete
这里是节点js代码
const express = require('express')
var mysql = require('mysql')
const bodyParser = require('body-parser')
const app = express()
var flash = require('connect-flash')
var session = require('express-session');
//...
app.use(session({ cookie: { maxAge: 60000 },
secret: 'woot',
resave: false,
saveUninitialized: false}));
const port = 3000
app.use('/public', express.static(__dirname + '/public'));
app.use('/Dashboard', express.static(__dirname + '/views'));
app.use(flash());
app.use(bodyParser.urlencoded({extended: false}))
app.set('view engine', 'pug')
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'audit',
timezone: 'Z'
})
connection.connect(function(err){
if (err) throw err;
connection.query("SELECT * FROM auditresult", function (err, result) {
if (err) throw err;
console.log('Data has been Exported successfully'+ result.length);
});
})
// DELETE
app.get('/delete/:id', function(req, res, next) {
connection.query("DELETE FROM auditresult WHERE id =' " +req.params.id+" ' ", function(err, result) {
res.redirect('Report')
})
})
// Edit
app.get('/Edit/:id', function(req, res, next){
var id= req.params.id;
var sql="SELECT * from auditresult where id= " +req.params.id+" ;"
connection.query(sql, function(err,items,fields){
res.render('Update',{ product: items[0]});
})
});
当我点击“编辑”时,我得到的是 URL“http://127.0.0.1:3000/Edit/%7Bitem.id%7D” 预先感谢您的帮助
我认为您需要在 jade 模板中更正此问题:
a(href='/Edit/' + item.id) 和 a(href='/delete/' + item.id)