#Node.jsからMySqlへのデータの更新
※自分用の覚え書きでございます。
req を受け取り、MySql へ post します。
###INSERT
app.js
app.post('/entry', (req, res) => {
connection.query(
'INSERT INTO tbl_items (name) VALUES (?)',
[req.body.itemName],
(error, results) => {
connection.query(
'SELECT * FROM tbl_items',
(error, results) => {
res.render('index.ejs', {items: results});
}
);
}
);
});
###UPDATE
app.js
app.post('/update/:id', (req, res) => {
connection.query(
'UPDATE tbl_items SET name = ? WHERE id = ?',
[req.body.itemName,req.params.id],
(error, results) => {
res.redirect('/index');
}
);
});
###DELETE
app.js
app.post('/delete/:id', (req, res) => {
connection.query(
'DELETE FROM tbl_items WHERE id = ?',
[req.params.id],
(error, results) => {
res.redirect('/index');
}
);
});