[英]deleting and updating a record in mysql using nodejs and express with ejs templating engine
Am just beginning my web dev journey and ran into a few problems.我刚刚开始我的 web 开发之旅并遇到了一些问题。 I am trying to delete and update any of the record in mysql using the following code:我正在尝试使用以下代码删除和更新 mysql 中的任何记录:
//deleting a record
Router.get('/delete', (req, res, next)=> {
db.query('DELETE FROM student WHERE student_id = ?',req.body.student_id, (err,rs)=>{
if(err) throw err;
else {
res.redirect('showStudents');
}
});
});
//editing a record
Router.get('/edit',(req, res, next)=>{
db.query('SELECT * FROM student WHERE `student_id`=?',[req.body], (err,rs)=>{
res.render('form',{Students: rs})
});
});
Router.post('/edit',(req, res, next)=>{
var params=[
req.body,
req.query.student_id
]
db.query('UPDATE student SET? WHERE student_id =?',params, (err,rs)=>{
if(!err)
res.redirect('showStudents')
else {
console.log(err);
}
});
});
This is the arrangement of the code in ejs:这是ejs中代码的排列方式:
<a href="/delete?student_id= <%= student.student_id %>" onClick=" return confirm ('Are you sure you?')">Delete</a>
<a href="/edit?student_id =<%= student.student_id %>">Edit</a>
running the delete code, I get this error below.运行删除代码,我在下面收到此错误。 With edit code, nothing changes.使用编辑代码,没有任何变化。
{ code: 'ER_PARSE_ERROR', errno: 1064, sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' { code: 'ER_PARSE_ERROR', errno: 1064, sqlMessage: "您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以了解在 '?' 附近使用的正确语法at line 1", sqlState: '42000', index: 0, sql: 'DELETE FROM student WHERE student_id =?'在第 1 行",sqlState:'42000',索引:0,sql:'从学生那里删除 student_id =?' } }
Please help me.请帮我。
Since you´re sending your parameters through a query string in the url you need to access them with req.query Ex.: var student_id= req.query.student_id;由于您通过 url 中的查询字符串发送参数,因此您需要使用 req.query 访问它们 例如:var student_id= req.query.student_id; (use req.body when the parameters are sent by form) (通过表单发送参数时使用req.body)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.