简体   繁体   中英

Express JS - deleting entry from mysql

I am using Express.js to build a small application. I have successfully implement the feature that allows me to add new entries to the database.

I am currently trying to delete an entry from a mysql db when a user clicks on a button. This is the code that gets executed:

client.query('DELETE FROM humans WHERE id= ?', [req.params.id], function(err, result) {
        if (err) {
            throw err;
        }

        res.redirect('/humans');

I know that the code executes properly because I get redirected to the correct page. Still, my entry is still in the database.

Deducing from the comments to your question I guess that you're sending the id from a form using a POST request. To find post params in req.body (if you're using the express.bodyParser middleware). Thus:

client.query('DELETE FROM humans WHERE id= ?', [req.body.id], function(err, result) {
  if (err) {
      throw err;
  }

  res.redirect('/humans');
});

If you're sending a GET request the param would be find in req.query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM