Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda