我想用帖子中的数据更新帖子,因为用户不必填写所有字段。 例: 我在表中有3列(id,内容,位置)。如果用户只想更改内容,则位置不应更改。
app.put("/posts", (req, res) => {
let body = req.body; // user filled id(because knows what posts he want to edit) and content, location leave empty
//So i want select for give data about location
let postsSql = "SELECT * FROM Posts";
let updateSql = "SET @id = ?;SET @content = ?;SET @location = ?;UPDATE Posts SET content = @content ,location = @location WHERE id = @id";
mysqlConnection.query(postsSql, body.id, (error, postsResult) => {
posts = postsResult[1][0];
});
let values = [
body.id,
body.content || posts.content,
body.location || posts.location
]
mysqlConnection.query(updateSql, values, (err, result) => {
if (!err) res.send("Updated");
}
我感觉自己做错了,如何以最佳方式做到这一点