Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Deleting single MySQL row with HTML and Node.js

I am trying to implement a way to delete a chosen row from my table. There is a Delete link at the end of each row which when clicked is meant to delete that row. When I run it and click the Delete link it does nothing. I don't get any errors, the console says 0 row(s) updated and the row remains. Not sure what I'm missing.

app.get('/device-list', function (req, res) {
  // query database to get all the devices
  let sqlquery = 'SELECT * FROM devices';

  // execute sql query
  db.query(sqlquery, (err, result) => {
    if (err) {
      res.redirect('/');
    }
    res.render('device-list.html', { availableDevices: result });
  });
});

app.get('/delete/:id', function (req, res, next) {
  var sql = 'DELETE FROM devices WHERE id = ?';
  var id = req.body.id;

  db.query(sql, [id], function (err, result) {
    if (err) {
      throw err;
    }
    console.log(result.affectedRows + ' row(s) updated');
  });
  res.redirect('/device-list');
});
<table>
  <thead>
    <th>Name</th>
    <th>Type</th>
    <th>Room</th>
  </thead>
  <tbody>
    <% availableDevices.forEach(function(device){ %>
    <tr>
      <td><%=device.Name %></td>
      <td><%=device.Type %></td>
      <td><%=device.Room %></td>
      <td><a href="/edit/<%=device.id%>">Edit</a></td>
      <td><a href="/delete/<%=device.id%>">Delete</a></td>
    </tr>
    <% }) %>
  </tbody>
</table>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are passing the id as a route parameter not as a part of a body. So you should access it using the params property.

 var id = req.params.id;

Give it a try

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!