我想写一个更新用户信息的接口,
UPDATE user_information SET 这里的参数不固定 where uid = ?
有可能是 name=? 或者 name=?, sex=? 或者 name=?, sex=?, qq=?
请怎么我应该如何实现?
app.all("/information/update", (req, res, next) => {
let sess = req.session;
let result = {
code: "0000",
data: {},
message: "fail",
detail: "",
function: "prize",
number: 0
};
let params = req.body;
let selectSql = "select name from user_information where uid = ?";
let updateSql = "UPDATE user_information SET name=? where uid = ?";
let name = helper.stringTrim(params.name) || "";
if(sess.loginUserInfo === undefined) {
result["detail"] = "用户没有登陆";
res.send(JSON.stringify(result));
} else {
let loginInformation = sess.loginUserInfo;
// console.log("loginInformation == ", loginInformation);
pool.getConnection((err, connection) => {
connection.query(
selectSql,
[loginInformation.uid],
(err, rows) => {
// console.log("rows ==", rows);
if(rows.length === 1) {
result["detail"] = "用户登陆成功";
connection.query(
updateSql,
[name, loginInformation.uid],
(err, rows) => {
if(err) {
result["detail"] = "更新数据失败";
res.send(JSON.stringify(result));
} else {
result["detail"] = "0001";
result["message"] = "success";
result["detail"] = "更新数据成功";
res.send(JSON.stringify(result));
}
}
);
} else {
result["detail"] = "用户信息错误";
res.send(JSON.stringify(result));
}
}
);
pool.releaseConnection(connection);
})
}
});