# 在/application/controllers/article.js中新增代码块 ``` async mod(req, res) { if (req.isPost) { //提交后 var post = req.body; var itemid = parseInt(req.query.itemid); if (!itemid) { res.render('msg', { code: 0, msg: '数据不存在', url: '' }); return; } //标题判断 var title = post.title.trim(); if (!title) { res.render('msg', { code: 0, msg: '标题不能为空', url: '' }); return; } //内容判断 var content = post.content.trim(); if (!content) { res.render('msg', { code: 0, msg: '内容不能为空', url: '' }); return; } //执行更新操作 let r = await model.u("update article SET ? where itemid=?", post, [itemid]); if (r) { res.render('msg', { code: 1, msg: '操作成功', url: '/article/index' }); } else { res.render('msg', { code: 0, msg: '操作失败', url: '' }); } return; } var itemid = parseInt(req.query.itemid); if (!itemid) { res.render('msg', { code: 0, msg: '数据不存在', url: '' }); return; } //从数据库中读取文章 let data = await model.find("SELECT * from article where itemid=?", [itemid]); //加载模板文件 res.render(req.TPL, { data: data }); } ``` # 新建对应模板文件/application/views/article/mod.html ``` <!doctype html> <html> <head> <meta charset="utf-8"> <title>编辑文章</title> </head> <body> <div style="text-align:center"> <h2>编辑文章 <span><a href="/article/index">返回</a></span></h2> <form action="/article/mod?itemid=<%=data.itemid%>" method="post"> <table width="100%" border="1"> <tbody> <tr> <td align="right">标题</td> <td align="left"><input type="text" name="title" id="" value="<%=data.title%>"></td> </tr> <tr> <td align="right">内容</td> <td align="left"><textarea name="content" id="" cols="30" rows="10"><%=data.content%></textarea> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="提交"></td> </tr> </tbody> </table> </form> </div> </body> </html> ``` # 显示结果 ![](https://img.kancloud.cn/bc/97/bc97b4e61daa774e30c5c0911c937408_897x350.png)