Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda