更新数据库
1.在api.js中添加代码如下:
// 修改
app.get("/update", function (req, res) { //创建路由,第一个参数是路由,第二个是函数
var sql = "update stuinfo set stuname=?,sex=?,stubirth=?,classid=? where id=?"; //查询myaql表里的数据条件
var params = [req.query.stuname, req.query.sex, req.query.stubirth, req.query.classid, req.query.id]; //获取用户输入的信息自定义名
conn.query(sql, params, function (err, rs) {//查询方法:1.查询语句,1.回调函数
if (err) throw err; //抛出错误
if (rs.affectedRows > 0) { //受影响的行大于0
res.send('{"code":"1","msg":"操作成功"}') //修改成功
} else {
res.send('{"code":"0","msg":"操作失败"}') //修改失败
}
})
})
2.前端index.js代码修改如下:
$.ajax({
"url": "http://127.0.0.1:8888/update",
"data": { "id": 2,"stuname":"王五","sex":"男","stubirth":"2004-09-15","classid":2 },
"dataType": "json",
"type": "get",
success: function (res) {
console.log(res)
}
})
3.访问http://nodejsdemo/,按【F12】查看console
node.js创建网站实例1_hifhf的博客-优快云博客
node.js创建网站实例2_hifhf的博客-优快云博客
node.js创建网站实例3_hifhf的博客-优快云博客
node.js创建网站实例4_hifhf的博客-优快云博客
node.js创建网站实例5_hifhf的博客-优快云博客