2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Node.js] Node.jsからMySqlへのデータの登録・更新・削除

Posted at

#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');
    }
  );
});
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?