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>